I'd like to parse this XML file:
<NameDefinitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../Documents/Calcs/FriendlyNames.xsd">
<NameDefinition>
<ID>/Temporary/EIC/EICWorksheetPP/WagesLessThanAdjustments</ID>
<Name>you have more income that doesn't count than income that counts</Name>
<BooleanPhrases>
<True><Text>you have more income that doesn't count than income that counts</Text></True>
<False><Text>you have more income that counts than income that doesn't count</Text></False>
<Blank><Text>we don't if you have more income that doesn't count than income that counts</Text></Blank>
</BooleanPhrases>
<BooleanQuestions>
<True><Text>Why do I have more income that doesn't count than income that counts?</Text></True>
<False><Text>Why do I have more income that counts than income that doesn't count?</Text></False>
<Blank><Text>Why don't you know if I have more income that doesn't count than income that counts?</Text></Blank>
</BooleanQuestions>
</NameDefinition>
<NameDefinition>
<ID>/Temporary/EIC/HaveInaccurateInfo</ID>
<Name>you told us your info is incorrect</Name>
<BooleanPhrases>
<True><Text>you told us some of your info is incorrect</Text></True>
<False><Text>you told us your info is correct</Text></False>
<Blank><Text>we don't know whether your info is correct</Text></Blank>
</BooleanPhrases>
<BooleanQuestions>
<True><Text>Why is some of my info incorrect?</Text></True>
<False><Text>Why is my info correct?</Text></False>
<Blank><Text>Why don't you know if my info is correct?</Text></Blank>
</BooleanQuestions>
</NameDefinition>
</NameDefinitions>
To get the values for ID and Name nodes. The data structure I'd like returned is Map(String,String) so ID is key and Name is value.
How can I accomplish this in Java 6?
Edit:
Here's my current implementation, however
static Map<String, String> parse(String pathToFriendlyNamesFile)
throws IOException,
XPathExpressionException {
XPath xpath = XPathFactory.newInstance().newXPath();
Map<String, String> map = new LinkedHashMap<String,String>();
InputStream file = null;
try {
file = new BufferedInputStream(Files.newInputStream(Paths.get(pathToFriendlyNamesFile)));
InputSource inputSource = new InputSource(file);
NodeList nodeIDList = (NodeList) xpath.evaluate("//NameDefinition/ID", inputSource, XPathConstants.NODESET);
NodeList nodeNameList = (NodeList) xpath.evaluate("//NameDefinition/Name", inputSource, XPathConstants.NODESET);
int nodeCount = nodeIDList.getLength();
System.out.println("Node count: "+nodeCount);
for(int i=0;i<nodeCount;i++) {
Node nodeID = nodeIDList.item(i);
Node nodeName = nodeNameList.item(i);
String id = nodeID.getTextContent();
String name = nodeName.getTextContent();
map.put(id, name);
}
} catch (IOException e) {
e.printStackTrace();
} catch (XPathExpressionException e) {
e.printStackTrace();
} finally {
file.close();
}
return map;
}
Exceptions:
java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:162)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:206)
at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
javax.xml.xpath.XPathExpressionException: java.io.IOException: Stream closed
at org.apache.xpath.jaxp.XPathImpl.evaluate(XPathImpl.java:481)
Caused by: java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:162)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:206)
at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
I've closed the stream but it's apparently still a problem?