'JAVA: Read xml with iterated child node names
To migrate from an old XML-based application to a database application I need to implement a converter that reads XML files and creates a file with insert queries
I've taken a look at common tutorials for java XML reading, but my issue is, that I do not have Nodes with the same name, but unique node names following a certain prefix.
Most Tutorial Examples follow a scheme like this:
<root>
<class>
<node></node>
<node></node>
<node></node>
</class>
</root>
which allows the usage of doc.getElementsByTagName("theName"). But in my case, the tagname is a prefix followed by unique identifier, like <theNodeName_A1>. Here is a sample of my XML. each <theNodeName_XX> contains multiple children and children of children.
<root>
<class>
<theNodeName_A1>
</theNodeName_A1>
<theNodeName_B3>
</theNodeName_B3>
</class>
</root>
My goal is to provide a function that does something like "doc.getElementsbyTagName(contains("theNodeName")) which would allow to iterate through each node and process child (subnodes) of each node.
How can I achieve this?
Solution 1:[1]
getElementsByTagName is not going to work for this. I think the easiest approach is to use XPath:
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList elements = (NodeList) xpath.evaluate(
"//class/*[contains(local-name(), '_')]",
doc, XPathConstants.NODESET);
int count = elements.getLength();
for (int i = 0; i < count; i++) {
Element element = (Element) elements.item(i);
String name = element.getTagName();
int underscore = name.indexOf('_');
String id = name.substring(underscore + 1);
// ...
}
You could also iterate over the child elements one by one, but it’s more involved:
NodeList classElements = doc.getElementsByTagName("class");
int classCount = classElements.getLength();
for (int i = 0; i < classCount; i++) {
Element classElement = (Element) classElements.item(i);
NodeList children = classElement.getChildNodes();
int childCount = children.getLength();
for (int j = 0; j < childCount; j++) {
Node child = children.item(j);
// IMPORTANT: Not all child nodes are Elements.
// DO NOT SKIP THIS CHECK!
if (child instanceof Element) {
Element element = (Element) child;
String name = element.getTagName();
int underscore = name.indexOf('_');
if (underscore > 0) {
String id = name.substring(underscore + 1);
// ...
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | VGR |
