Let's take a simple XML document:
<x>
<e>
<e>
<e>Whatever 1</e>
</e>
</e>
<e>
<e>
<e>Whatever 2</e>
</e>
</e>
<e>
<e>
<e>Whatever 3</e>
</e>
</e>
</x>
Using the standard org.w3c.dom, I can get the nodes in X by doing..
NodeList fullnodelist = doc.getElementsByTagName("x");
But if I want to return the next set of "e" I try to use something like ..
Element element = (Element) fullnodelist.item(0);
NodeList nodes = pelement.getElementsByTagName("e");
Expecting it to return "3" nodes (because there are 3 sets of "e"), but instead, it returns "9" - becuase it gets all entries with "e" apperently.
This would be fine in the above case, because I could probably iterate through and find what I'm looking for. The problem I'm having is that when the XML file looks like the following:
<x>
<e>
<pattern>whatever</pattern>
<blanks>
<e>Something Else</e>
</blanks>
</e>
<e>
<pattern>whatever</pattern>
<blanks>
<e>Something Else</e>
</blanks>
</e>
</x>
When I request the "e" value, it returns 4, instead of (what i开发者_StackOverflow中文版 expect) 2.
Am I just not understanding how the DOM parsing works? Typically in the past I have used my own XML documents so I would never name the items like this, but unfortunately this is not my XML file and I have no choice to work like this.
What I thought I would do is write a loop that "drills down" nodes so that I could group each node together...
public static NodeList getNodeList(Element pelement, String find)
{
String[] nodesfind = Utilities.Split(find, "/");
NodeList nodeList = null;
for (int i = 0 ; i <= nodesfind.length - 1; i++ )
{
nodeList = pelement.getElementsByTagName( nodesfind[i] );
pelement = (Element)nodeList.item(i);
}
// value of the nod we are looking for
return nodeList;
}
.. So that if you passed "s/e" into the function, it would return the 2 nodes that I'm looking for (or elements, maybe I'm using the terminology incorrect?). instead it returns all of the "e" nodes within that node.
I'm using J2SE for this, so options are rather limited. I can't use any third party XML Parsers.
Anyway, if anyone is still with me and has a suggestion, it would be appreciated.
If you are manual traversing the Xml, try using a variable which increments as you encounter each "e" tag, then decrements as you leave it.
If the source follows the above example you gave, you could use a simple if statement to make sure that the counter is equal to 2 before performing an action (assuming it started at 0)
I might have slightly misunderstood your exact problem, but I hope this helps.
A better solution than a counter would be to check each of the returned nodes to see if they have the correct parent.
getChildNodes()
returns not just all children, but all descendants (i.e. complete subtree).
Element element = doc.getDocumentElement();
NodeList nodeList = element.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++ ) {
if (nodeList.item(i).getNodeType() == Node.ELEMENT_NODE && nodeList.item(i).getParentNode().isSameNode(element)) {
Element childElement = (Element) nodeList.item(i);
if (childElement.getTagName().equals("someTagName")) {
handleSomeTag(childElement);
} else if (childElement.getTagName().equals("someOtherTagName")) {
handleSomeOtherTag(childElement);
}
}
}
getElementsByTagName()
returns not just all children with that tagname, but all descendants with that tagname (i.e. all tags with that name in the complete subtree).
Element element = doc.getDocumentElement();
NodeList nodeList = element.getElementsByTagName("someTag");
for (int i = 0; i < nodeList.getLength(); i++ ) {
if (nodeList.item(i).getNodeType() == Node.ELEMENT_NODE && nodeList.item(i).getParentNode().isSameNode(element)) {
Element childElement = (Element) nodeList.item(i);
handleSomeTag(childElement);
}
}
nodeList = element.getElementsByTagName("someOtherTag");
for (int i = 0; i < nodeList.getLength(); i++ ) {
if (nodeList.item(i).getNodeType() == Node.ELEMENT_NODE && nodeList.item(i).getParentNode().isSameNode(element)) {
Element childElement = (Element) nodeList.item(i);
handleSomeOtherTag(childElement);
}
}
You need to learn about XPath. There's an XPathFactory in stock Java 1.5, and you can create an XPath to name the particular 'e''s that you want.
精彩评论