我想获取每个元素的第一个子元素的元素名称和值。但我得到空值。我对 XML 很陌生,我很困惑,只是想用漂亮的字体来做一些事情。根大约有 15 个孩子,他们都称为“DEvent”,我希望我的输出是:
活动:周三步道徒步
事件:弓 - 狗泥!
等等
<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>LindaRadimecky@state.mn.us</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>
我的java显示结果-
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);
}