Read data from xml at url in java - java

I need to be able to pull out the USD conversion from Euro using this xml:
http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml
I've gotten it to work if I manually download the xml and parse to the data I need from there, but I'm not sure how to grab it directly from the url.
Thank you

If i understand well, you are having trouble obtaining the xml content directly from the URL, is that right?
If so, this might help you.
// the SAX way:
XMLReader myReader = XMLReaderFactory.createXMLReader();
myReader.setContentHandler(handler);
myReader.parse(new InputSource(new URL(url).openStream()));
// or if you prefer DOM:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new URL(url).openStream());
It applies well to a service or stand alone context, but if you are trying to access the XML from your WEB tier you might consider an AJAX approach.

Cant make any easier than that:
package com.danielmarreco;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.InputStream;
import java.net.URL;
public class Main {
public static void main(String[] args) {
try {
InputStream is = new URL("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml").openStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(is);
NodeList nodeList = doc.getElementsByTagName("Cube");
for (int i = 0; i < nodeList.getLength(); i ++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
if(element.getAttribute("currency").equals("BRL"))
System.out.println("1 EUR = " + element.getAttribute("rate") + " BRL");
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}

Related

Parsing an XML with 2 default namespace in Java using DOM parser

Thanks in advance.
I have a xml with 2 default namespace at rootlevel and then as element level.
<?xml version="1.0" encoding="UTF-8"?>
<Msg xmlns="http://www.geological.com">
<header>
<date>08-08-2021</date>
<jur>US</jur>
</header>
<Demographic xmlns="urn:com.test:009">
<geoData>
<id>DL89716</id>
<name>North</name>
</geoData>
</Demographic>
</Msg>
I am using Java DOM parser to read this xml and fetch value of "id".
Still I am getting value as null
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
dbFactory.setNamespaceAware(true);
document = dBuilder.parse(new InputSource(new StringReader(xmlPayLoad)));
document.normalize();
XPathEvaluator xPathObj = (XPathEvaluator)XPathFactory.newInstance(NamespaceConstant.OBJECT_MODEL_SAXON).newXPath();
xPathObj.setNamespaceContext(new MyNameSpace());
xPathObj.getStaticContext().setDefaultElementNamespace("http://www.geological.com");
XPathExpression expr = xPathObj.compile(xpath);
Object result = expr.evaluate(document, XPathConstants.NODESET);
NodeList nodeList = (NodeList) result;
private static class MyNameSpace implements NamespaceContext {
//The lookup for the namespace uris is delegated to the stored document.
public String getNamespaceURI(String prefix) {
if (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) {
return null;
} if("ns".equals(prefix)){
return "urn:com.test:009";
}
}
public String getPrefix(String namespaceURI) {
return sourceDocument.lookupPrefix(namespaceURI);
}
#SuppressWarnings("rawtypes")
public Iterator getPrefixes(String namespaceURI) {
return null;
}
}
Here is a fully functional example:
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class NamespacesExample {
private static String xmlPayLoad =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<Msg xmlns=\"http://www.geological.com\">\n" +
" <header>\n" +
" <date>08-08-2021</date>\n" +
" <jur>US</jur>\n" +
" </header>\n" +
" <Demographic xmlns=\"urn:com.test:009\">\n" +
" <geoData>\n" +
" <id>DL89716</id>\n" +
" <name>North</name>\n" +
" </geoData>\n" +
" </Demographic>\n" +
"</Msg>";
public static void main(String[] args) {
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = null;
dbFactory.setNamespaceAware(true);
dBuilder = dbFactory.newDocumentBuilder();
Document document = dBuilder.parse(new InputSource(new StringReader(xmlPayLoad)));
final Map<String, String> ns = new HashMap<>();
ns.put("geo", "http://www.geological.com");
ns.put("test", "urn:com.test:009");
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(new MyNameSpace(ns));
XPathExpression expr = xpath.compile("/geo:Msg/test:Demographic/test:geoData/test:id/text()");
String result = (String) expr.evaluate(document, XPathConstants.STRING);
System.out.println("Result: " + result);
} catch (ParserConfigurationException | IOException | XPathExpressionException | SAXException e) {
e.printStackTrace();
}
}
private static class MyNameSpace implements NamespaceContext {
private final Map<String, String> ns;
MyNameSpace(Map<String, String> ns) {
this.ns = new HashMap<>(ns);
this.ns.put(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI);
this.ns.put(XMLConstants.XMLNS_ATTRIBUTE, XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
}
#Override
public String getNamespaceURI(String prefix) {
if(prefix == null) {
throw new IllegalArgumentException();
}
final String uri = ns.get(prefix);
return uri == null ? XMLConstants.NULL_NS_URI : uri;
}
#Override
public String getPrefix(String namespaceURI) {
throw new UnsupportedOperationException();
}
#Override
public Iterator getPrefixes(String namespaceURI) {
throw new UnsupportedOperationException();
}
}
}
Some remarks:
The order of these statements is significant. Otherwise there is no namespace awareness:
dbFactory.setNamespaceAware(true);
dBuilder = dbFactory.newDocumentBuilder();
Why do you use XPathEvaluator? There is no need for that. Using the interface XPath is enough.
What is NamespaceConstant.OBJECT_MODEL_SAXON? At least in this example, we don't need it.
With my generic NamespaceContext implementation you can use a simple Map<String, String> to define the namespace prefixes for XPATH evaluation.
Dedicated prefixes should be used for each namespace used in the XML, regardless of how the namespace is declared in the XML document.
Then you can use this XPATH expression to extract the id: /geo:Msg/test:Demographic/test:geoData/test:id/text()
Output is:
Result: DL89716
I would use //*:id as the path if you know there is a single id element you are looking for, or at least there are not several id elements from different namespaces and you want to select only those from one namespace.
If you only want to select those from one namespace (e.g. urn:com.test:009) then set that as the default namespace and you can simply use //id or //Demographic/geoData/id.
Only if you really need to select elements from different namespaces you need to bind prefixes.
With the Java 8 built-in JAXP XPathFactory and a not namespace aware DocumentBuilderFactory the following also works for me to select the id element node as then the default namespace declarations are ignored when building the DOM tree:
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(false);
DocumentBuilder jaxpDocumentBuilder = documentBuilderFactory.newDocumentBuilder();
Document domDoc = jaxpDocumentBuilder.parse(new File("sample1.xml"));
XPathFactory jaxpXPathFactory = XPathFactory.newInstance();
Node resultDomNode = (Node)jaxpXPathFactory.newXPath().evaluate("/Msg/Demographic/geoData/id", domDoc, XPathConstants.NODE);
I think in your original code, once you build a namespace aware DOM, with your use of the Saxon API, you could get away with setting
xPathObj.getStaticContext().setUnprefixedElementMatchingPolicy(UnprefixedElementMatchingPolicy.ANY_NAMESPACE);
and not to set up any namespace context nor default element namespace.
The other way with Saxon 10 and XPath to match elements in any namespace with selectors like id I could find is to dive into its low-level API to mix it with the s9api:
Processor processor = new Processor(false);
DocumentBuilder docBuilder = processor.newDocumentBuilder();
XdmNode input = docBuilder.build(new File("sample1.xml"));
NodeInfo contextItem = input.getUnderlyingNode();
XPathEvaluator xpathEvaluator = new XPathEvaluator(processor.getUnderlyingConfiguration());
IndependentContext independentContext = new IndependentContext();
independentContext.setUnprefixedElementMatchingPolicy(UnprefixedElementMatchingPolicy.ANY_NAMESPACE);
xpathEvaluator.setStaticContext(independentContext);
XPathExpression expression = xpathEvaluator.createExpression("/Msg/Demographic/geoData/id");
NodeInfo resultInfo = (NodeInfo) expression.evaluateSingle(expression.createDynamicContext(contextItem));
XdmNode resultNode = new XdmNode(resultInfo);
System.out.println(resultNode);

update xml with xpath

I want to change the rating of jojo restaurant only , i tried this code but didn't work please help. Below is my xml document and the code I have.
<city>
<beirut>
<restaurant>
<name>sada</name>
</restaurant>
</beirut>
<jbeil>
<restaurant>
<name>toto</name>
<rating>4.3/5</rating>
</restaurant>
<restaurant>
<name>jojo</name>
<rating>4.3/5</rating>
</restaurant>
</jbeil>
<sour>
<restaurant>
<name>sada</name>
</restaurant>
</sour>
</city>
Code:
try {
File inputFile = new File("src/xpath/josephXml.xml");
DocumentBuilderFactory dbFactory
= DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
XPath xPath = XPathFactory.newInstance().newXPath();
String expression = "/City/Jbeil/Restaurants/Restaurant[name='Feniqia']/rating";
Element e = (Element)xPath.evaluate(expression, doc, XPathConstants.NODE);
if (e != null){
e.setTextContent("5/5");
}
} catch (ParserConfigurationException | SAXException | IOException e) {
}
if you only need to write the result (there are other ways):
import java.io.File;
import org.w3c.dom.Document;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
try
{
Transformer trans = TransformerFactory.newInstance().newTransformer();
Result out = new StreamResult(new File("result.xml"));
Source in = new DOMSource(doc);
trans.transform(in, out);
}
catch (Exception exe)
{
System.err.println(exe.getMessage());
}
First, your XPath did not align to your posted XML (no <restaurants> or Feniqia name) and all should have been lower case. Possibly you extracted a piece from large xml document. For demonstration, I used the jojo restaurant.
Second, XPath does not itself update an XML, you will need to create a new document. Simply add a transformation at end:
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import javax.xml.xpath.*;
import java.io.File;
import java.io.IOException;
import org.xml.sax.*;
import org.w3c.dom.*;
public class RatingUpdate {
public static void main(String [] args) {
try {
File inputFile = new File("src/xpath/josephXml.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
XPath xPath = XPathFactory.newInstance().newXPath();
String expression = "/city/jbeil/restaurant[name='jojo']/rating";
Element e = (Element)xPath.evaluate(expression, doc, XPathConstants.NODE);
if (e != null){
e.setTextContent("5/5");
}
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.transform(new DOMSource(doc), new StreamResult(new File("src/xpath/josephXml_update.xml")));
} catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException |
TransformerException e) {
}
}
}

Writing (overriding) same XML file that I read

I made a method to read XML files, but now I need to write or override that same XML file with the same tags, nodes and objects, but with different data inside child nodes.
First I want to make read and write working and then I have some ideas to maybe put the whole XML file into a buffer. Then I could put the whole XML under one class and just write that class again into the same XML this is just an idea, any suggestion or idea is welcome.
My XML file looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Document Root -->
<DATA>
<Settings USERNAME="test" PASSWORD="test" STATUS="active" / >
</DATA>
This is my code for reading:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class Read {
private final static String SETTINGS_LINE = Settings;
public void readXML() {
try {
File xmlFile = new File("Test.xml");
DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
Document doc = documentBuilder.parse(xmlFile);
// Normalize the XML file
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getDocumentElement().getChildNodes();
for(int temp = 0; temp < nodeList.getLength(); temp++) {
Node node = nodeList.item(temp);
if(node instanceof Element && node.getNodeName() == SETTINGS_LINE) {
Element settings = (Element) node;
System.out.println("User" +settings.getAttribute("USERNAME"));
System.out.println("Password" +settings.getAttribute("PASSWORD"));
System.out.println("Status" +settings.getAttribute("STATUS"));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
And this is the code for writing that is not working:
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Modify {
private final static String SETTINGS_LINE = "Settings";
public static void main(String argv[]) {
try {
String filepath = "test.xml";
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
// Normalize the XML File
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getDocumentElement().getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node instanceof Element && node.getNodeName() == SETTINGS_LINE) {
Element settings = (Element) node;
if("USERNAME".equals(node.getChildNodes())){
node.setTextContent("mivnadic");
}
}
// Write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(filepath));
transformer.transform(source, result);
}
System.out.println("File saved");
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (SAXException sae) {
sae.printStackTrace();
}
}
}

java xml parsing dblp

this is the xml file
please how to parse the tag author example we dont know how many author for each inproceeding ?
<?xml version="1.0" encoding="ISO-8859-1"?>
<dblp>
<inproceedings mdate="2014-01-18" key="series/sci/AzzagL13">
<author>Hanane Azzag</author>
<author>Mustapha Lebbah</author>
<title>A New Way for Hierarchical and Topological Clustering.</title>
<pages>85-97</pages>
<year>2011</year>
<booktitle>EGC (best of volume)</booktitle>
<ee>http://dx.doi.org/10.1007/978-3-642-35855-5_5</ee>
<crossref>series/sci/2013-471</crossref>
<url>db/series/sci/sci471.html#AzzagL13</url>
</inproceedings>
<inproceedings mdate="2014-01-18" key="series/sci/RabatelBP13">
<author>Julien Rabatel</author>
<author>Sandra Bringay</author>
<author>Pascal Poncelet</author>
<title>Mining Sequential Patterns: A Context-Aware Approach.</title>
<pages>23-41</pages>
<year>2011</year>
<booktitle>EGC (best of volume)</booktitle>
<ee>http://dx.doi.org/10.1007/978-3-642-35855-5_2</ee>
<crossref>series/sci/2013-471</crossref>
<url>db/series/sci/sci471.html#RabatelBP13</url>
</inproceedings>
</dblp>
Use Xpath, it's fast and powerfull , these lines for your example return 5 lines
Code:
final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new FileInputStream("input.xml"));
final XPath xPath = XPathFactory.newInstance().newXPath();
final NodeList nodeList = (NodeList) xPath.compile("//author").evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
}
Displays:
Hanane Azzag
Mustapha Lebbah
Julien Rabatel
Sandra Bringay
Pascal Poncelet
Following code parse using apache digester which is commonly used while parsing in real projects. Nice one from apache community
// Updated code as per you need.
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import org.apache.commons.digester.Digester;
import org.apache.commons.digester.Rule;
import org.apache.commons.digester.Rules;
import org.xml.sax.InputSource;
public class Parsing {
public static void main(String[] args) throws Exception{
InputStream data = new FileInputStream("E:\\workspace\\trunk\\Parsing\\src\\data.xml");
byte[] b = new byte[data.available()];
// data.read(b);
Digester digester = new Digester();
//Genearting Array list while encountering dblp xpath
digester.addObjectCreate("dblp", HashMap.class);
digester.addObjectCreate("dblp/inproceedings", ArrayList.class);
//Calling add method while encountering author xpath
AuthorRule rule = new AuthorRule();
digester.addRule("dblp/inproceedings/author", rule);
digester.addRule("dblp/inproceedings/title", rule);
digester.addRule("dblp/inproceedings", rule);
HashMap parsedData = (HashMap) digester.parse(data);
Iterator<Entry<String, ArrayList>> dataItr = parsedData.entrySet().iterator();
while(dataItr.hasNext()){
Entry<String, ArrayList> entry = dataItr.next();
System.out.println("Title : " + entry.getKey() + ", Authors" + entry.getValue().toString());
}
}
private static class AuthorRule extends Rule{
String currentTitle = "";
#Override
public void body(String namespace, String name, String text)
throws Exception {
HashMap object = (HashMap) digester.peek(1);
ArrayList authors = (ArrayList) digester.peek(0);
if(name.equals("title")){
currentTitle = text;
}
else if(name.equals("author")){
authors.add(text);
}
}
#Override
public void end(String namespace, String name) throws Exception {
HashMap object = (HashMap) digester.peek(1);
ArrayList authors = (ArrayList) digester.peek(0);
if(name.equals("inproceedings")){
object.put(currentTitle, authors);
}
}
}
}
output::
Title : A New Way for Hierarchical and Topological Clustering., Authros[Hanane Azzag, Mustapha Lebbah]
Title : Mining Sequential Patterns: A Context-Aware Approach., Authros[Julien Rabatel, Sandra Bringay, Pascal Poncelet]
there are number of ways, e.g. via DOM:
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
public class XmlAuthorReader {
public static void main(String argv[]) {
try {
File fXmlFile = new File(<filePath>);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
NodeList nList = doc.getElementsByTagName("author");
System.out.println(nList.getLength()+ " author(s) found");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("Author: " + nNode.getTextContent());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
you can find more variants here: http://www.mkyong.com/tutorials/java-xml-tutorials/

Java Dialog box to allow use to pick file

This is the code I have at present:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XMLReader extends JFrame
{
JFileChooser _fileChooser = new JFileChooser();
JPanel content = new JPanel();
//... Create menu elements (menubar, menu, menu item)
JMenuBar menubar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem openItem = new JMenuItem("Open...");
int retval = _fileChooser.showOpenDialog(XMLReader.this);
//... The user selected a file, get it, use it.
public static void main(String argv[])
{
ArrayList timeStamp = new ArrayList();
ArrayList Y = new ArrayList();
File file = XMLReader.this;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
System.out.println("Root element " + doc.getDocumentElement().getNodeName());
NodeList nodeLst = doc.getElementsByTagName("reading");
System.out.println("Share Data");
for (int s = 0; s < nodeLst.getLength(); s++) {
Node fstNode = nodeLst.item(s);
if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
Element fstElmnt = (Element) fstNode;
NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("timeStamp");
.
.
.
}
I am trying to get a dialog box to allow the user to pick the XML they want parsed. I know the parsing works as I had hard coded in the file before.
I would also like to return the ArrayLists so that I can use them as the inputs to another class is this possible (at the moment I am only printing them to screen)?
System.out.println(timeStamp);
System.out.println(Y);
Can I use a return statement and if so how to I set up the class that I want to use them in?
The dialog box serve to retrive the path
I see that You are using the awt and dom so:
public Document loadXmlFile(Frame frame, DocumentBuilder docBuilder, String startPath) {
FileDialog fd = new FileDialog(frame, "Loadxml-title", FileDialog.LOAD);
//Add type filter
fd.setDirectory(startPath));
fd.show();
String file = fd.getFile();
if(file == null) {
return null;
}
return docBuilder.parse(file);
}
Q2: "Error message when compiling, can't have DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); with throw or catch exception."
This error is raised bu compiler because You don't have the block try & catch in method main.
The definition of DocumentBuilderFactory.newInstace(); says that this method throw the FactoryConfigurationError. In Java when method throw some error and it is not a RuntimeException you need to throw it in method definition or add the try catch block to handle it.
DocumentBuilderFactory dbf = null;
try {
dbf = DocumentBuilderFactory.newInstance();
} Exception(FactoryConfigurationError e){
e.printStackTrace();
}

Categories

Resources