【发布时间】:2021-08-26 15:47:47
【问题描述】:
使用以下示例解析 XML 文件。 能够让它与从不同地方采集的样本一起工作,但它并没有向我解释为什么以下失败是这篇文章的目标。
这是正在执行的代码。
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class XMLQuestions
{
public static void main(String argv[])
{
try
{
String filepath = "c:\\Downloads\\DummyData.xml";
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filepath);
doc.setXmlStandalone(true);
NodeList allDeliveryLocations = doc.getElementsByTagName("DeliveryLocations");
for(int j=0; j < allDeliveryLocations.getLength();j++)
{
Element deliveryLocationElement = (Element) allDeliveryLocations.item(j);
Node deliveryLocationNode = (Node) allDeliveryLocations.item(j);
System.out.println("Get Element & Node Name");
System.out.println(" deliveryLocationElement : " + deliveryLocationElement.getNodeName());
System.out.println(" deliveryLocationNode : " + deliveryLocationNode.getNodeName());
System.out.println("");
System.out.println("GetFirstChild()");
Node deliveryLocationChild = deliveryLocationNode.getFirstChild();
System.out.println(" Node Name : " + deliveryLocationChild.getNodeName());
System.out.println(" Node Value : " + deliveryLocationChild.getNodeValue() );
System.out.println("");
System.out.println("GetNextSibling()");
deliveryLocationChild = deliveryLocationChild.getNextSibling();
System.out.println(" Node Name : " + deliveryLocationChild.getNodeName());
System.out.println(" Node Value : " + deliveryLocationChild.getNodeValue());
System.out.println("");
}
System.out.println("Done");
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
}
catch (IOException ioe) {
ioe.printStackTrace();
} catch (SAXException sae) {
sae.printStackTrace();
}
}
}
用于测试的示例 XML。
<?xml version="1.0" encoding="UTF-8"?>
<AllStorage>
<NorthAmerica>
<EastCoast>
<DeliveryLocations>
<Location>North East </Location>
<Item1>Full</Item1>
<Item2>Empty</Item2>
<Item3 attr1="1" attr2="2" />
<Item4 istransferable="true">
<States>
<State>
<NewYork>
<UpTown>
<TimeOfDelivery>Morning</TimeOfDelivery>
<DeliveryLocation>back</DeliveryLocation>
</UpTown>
</NewYork>
</State>
<State>
<NewYork>
<UpTown>
<TimeOfDelivery>Evening</TimeOfDelivery>
<DeliveryLocation>side</DeliveryLocation>
</UpTown>
</NewYork>
</State>
<State>
<NewYork>
<UpTown>
<TimeOfDelivery>Afternoon</TimeOfDelivery>
<DeliveryLocation>front</DeliveryLocation>
</UpTown>
</NewYork>
</State>
</States>
</Item4>
</DeliveryLocations>
<DeliveryLocations>
<Location>South East </Location>
<Item1>Totally Full </Item1>
<Item2>Half Empty</Item2>
<Item3 attr1="5" attr2="6" />
<Item4 istransferable="true">
<States>
<State>
<Florida>
<UpTown>
<TimeOfDelivery>Early Morning</TimeOfDelivery>
<DeliveryLocation>front</DeliveryLocation>
</UpTown>
</Florida>
</State>
<State>
<Florida>
<UpTown>
<TimeOfDelivery>MidDay</TimeOfDelivery>
<DeliveryLocation>back</DeliveryLocation>
</UpTown>
</Florida>
</State>
<State>
<Florida>
<UpTown>
<TimeOfDelivery>Midnight</TimeOfDelivery>
<DeliveryLocation>back</DeliveryLocation>
</UpTown>
</Florida>
</State>
</States>
</Item4>
</DeliveryLocations>
</EastCoast>
</NorthAmerica>
</AllStorage>
执行这些行,有助于查看是否可以使用元素或节点。
Element deliveryLocationElement = (Element) allDeliveryLocations.item(j);
Node deliveryLocationNode = (Node) allDeliveryLocations.item(j);
这是上面生成的输出。
问题 1:
是什么决定了使用元素还是节点?它会是人们期望执行的方法或要检索的数据吗?
接下来,执行以下代码行。
System.out.println("GetFirstChild()");
Node deliveryLocationChild = deliveryLocationNode.getFirstChild();
System.out.println(" Node Name : " + deliveryLocationChild.getNodeName());
System.out.println(" Node Value : " + deliveryLocationChild.getNodeValue() );
System.out.println("");
这是生成的输出。
由于 Location 是 DeliveryLocations 的子代,因此我应该会看到
Node Name : Location
Node Value : North East
问题 2:
为什么 getFirstChild() 没有按预期返回 Location?是否需要额外调用?
接下来,执行以下代码行。
System.out.println("GetNextSibling()");
deliveryLocationChild = deliveryLocationChild.getNextSibling();
System.out.println(" Node Name : " + deliveryLocationChild.getNodeName());
System.out.println(" Node Value : " + deliveryLocationChild.getNodeValue());
System.out.println("");
这是生成的输出。
好的,这次我打印出 Location,但它是在 getNextSibling() 之后,并且值仍然为 null。
问题 3:
为什么需要同时调用 getFirstChild() 和 getNextSibling() 才能看到第一个智利?
为什么 getNextSibling() 没有打印出 Item1 而不是 null?
我认为获得这些问题的答案将有助于更好地了解正在发生的事情以及为什么需要某些调用。
【问题讨论】: