I'm used to C++ libraries for xml parsing, namely rapidxml and tinyxml. The java built in DOM parser makes absolutely no sense to me. After some struggle I was able to store the root node I need, but now I want to find a tag with a specific id.
Here is a sample xml:
<?xml version="1.0" encoding="UTF-8"?>
<adventure title="Test Adventure" author="Tester">
<enemies>
<enemy id="e1" difficulty="1">Enemy1</enemy>
</enemies>
<story>
<narration id="n1" action="d1">It's adventure time!</narration>
<decision id="d1">
<description>Would you like to participate?</description>
<choice action="n2">Yes, please.</choice>
<choice action="n3">Not the slightest.</choice>
</decision>
<narration id="n2" action="end">Great choice!</narration>
<narration id="n3" action="end">Okay...</narration>
</story>
</adventure>
I have the <story> node stored in a Node instance. From there, I want to find nodes wiith a specific id (let's say the 'n3' node).
I want to use this method:
private Node findNode(String id) {
Node node = storyNode.getFirstChild();
while ( node != null ) {
Element elem = (Element)node;
if ( elem.getAttribute("id") == id ) {
return node;
}
node = node.getNextSibling();
}
return null;
}
This would be my way to go with a C++ library, but this is very far from working... I don't understand why I have to typecast for such simple task. Why can't the Node have a method to get attributes with specifying the attribute name. The getFirstChild() doesn't even seem to return the first child...
The wrapper classes I found online are either 30 loops deep, or even harder to use. Why is this so complicated? What am I doing wrong?
NodeLists which you have to iterate through, but not with an iterator, because the only way to get a node out is with the.item(index)method, which returns aNode, which might be anElementand it goes on and on and on... I'd need three methods, one which can grab the next sibling, one to return a specific child element, or the first one if it's called without a parameter, and a third to read a specific attribute. I could not make this more difficult if I wanted to, something is really wrong here.IterableNodeList...my gut feeling is it was designed and written by engineers...Great on paper...not so much for the people who actually have to use it ;)