I thoroughly search answer to my question but didn't get it. My task is something like that. I have an XML:
<?xml version="1.0" encoding="utf-8" ?>
<users>
<user id="1">
<login>james.bond</login>
<password>12345</password>
<enabled>true</enabled>
</user>
<user id="2">
<login>john.doe</login>
<password>67890</password>
<enabled>false</enabled>
</user>
</users>
I want to parse it to HashMap where keys and values should be something like that (node names through a period as a keys and values of these nodes as a values in map):
K: users.user.1.login V: james.bond
K: users.user.1.password V: 12345
etc...
On the very moment I have:
public Map<String, String> parse(File file) {
Document doc = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
dbf.setIgnoringComments(true);
doc = db.parse(file);
} catch (ParserConfigurationException pce) {
System.out.println("Parser was not configured properly [LOG IT!]");
} catch (IOException io) {
System.out.println("Cannot read input file [LOG IT!]");
} catch (SAXException se) {
System.out.println("Problem parsing the file [LOG I!]");
} catch (IllegalArgumentException ae) {
System.out.println("Please specify an XML source [LOG IT!]");
}
Element root = doc.getDocumentElement();
return stepThrough(root);
}
private Map<String, String> stepThrough(Node node) {
String nodeName = node.getNodeName();
if (!nodeName.equals("#text")) {
buildedKey.append(nodeName).append(".");
}
if (node.getNodeValue() != null && !node.getNodeValue().matches("^\\W*$")) {
parsedMap.put(buildedKey.toString(),node.getNodeValue());
}
if (node.getNodeType() == Node.ELEMENT_NODE) {
if (node.hasAttributes()) {
NamedNodeMap startAttr = node.getAttributes();
for (int i = 0; i < startAttr.getLength(); i++) {
Node attr = startAttr.item(i);
buildedKey.append(attr.getNodeValue()).append(".");
}
}
}
for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
stepThrough(child);
}
return parsedMap;
}
And I have last key in my map for example: users.user.1.login.password.enabled.user.2.login.password.enabled
In other words all node names are concatenating.
Would you help me? Thanks!
P.S.: I have one restriction and I can not use libs like XStream...