Id like to get the element name and value of the first child of each element. But I'm getting null values. Im pretty new to XML and I am stumped, just trying to get something working with pretty font. Theres about 15 children to the root, and theyre all called "DEvent", I would like my output to be:
Event: Wednesday Trail Walks
Event: BOW - Dog Mushing!
etc.
<DNREvents>
<DEvent>
<event eid="1">Wednesday Trail Walks</event>
<date>2/03/2016</date>
<time>1-2:30 PM</time>
<description>...</description>
<location>Brown's Creek State Trail</location>
<cost>Free</cost>
<infoPhone>651-231-6968</infoPhone>
<infoEmail>[email protected]</infoEmail>
</DEvent>
<DEvent>
<event eid="2">BOW - Dog Mushing!</event>
<date>2/04/2016 thru 2/07/2016</date>
<time>Unknown</time>
<description>
This off the grid adventure is for those who want to actively participate in every aspect of learning to run your own small team of sled dogs through miles and miles of beautiful trails in the remote wilderness of northern Minnesota. Limited to 4 participants
</description>
<location>Grand Marais/Hovland</location>
<cost>$895</cost>
<infoPhone>218-370-0283</infoPhone>
<infoEmail>linda@points_unknown.com</infoEmail>
</DEvent>
<DEvent>...</DEvent>
<DEvent>...</DEvent>
<DEvent>...</DEvent>
<DEvent>...</DEvent>
<DEvent>...</DEvent>
<DEvent>...</DEvent>
<DEvent>...</DEvent>
<DEvent>...</DEvent>
<DEvent>...</DEvent>
<DEvent>...</DEvent>
<DEvent>...</DEvent>
<DEvent>...</DEvent>
<DEvent>...</DEvent>
</DNREvents>
My java to show the results-
Element DNR = root;
NodeList EventList=DNR.getElementsByTagName("DEvent");
for (int i=0; i< EventList.getLength();i++)
{
Node thisNode = EventList.item(i);
Node child = thisNode.getFirstChild();
String x = child.getNodeValue();
System.out.println(thisNode+ x);
}
Here is a working example. The firstChild in your code was a #Text node and you needed to get the nextSibling() of that node (So your event node is not the first node after all):
Be aware that this line will be fragile if you don't do additional null checking to verify the xml is well formed with the proper child nodes. And, this doesn't enforce the order of the child nodes in the xml is in the order you expect:
With that said, I think a better implementation would be to use XPath, which allows you to select the nodes you want without traversing manually down the tree and is more compact and 'fail safe':