我希望元素节点让文本节点更加环绕。根据{{3}}元素包含文本节点,反之亦然,文本节点应该由元素节点包围。
我需要Element-node( openingTime )来获取属性( day ),但NodeList(从节点 openingHours <调用的getChildNodes()) / strong>)只给我文本节点,其父节点是元素节点( openingHours ),但不是文本节点周围的节点,而是上面的节点。如果有人问,我需要使用DOM,因为我已经理解它,它是最快的。
这是XML:
<?xml version="1.0"?>
<stores>
<store name = "the Name of the Store">
<category>2nd Hand</category>
<locationAdress>sonstwo 18b, 12345 Bla</locationAdress>
<telephoneNumber>1234567812</telephoneNumber>
<openingHours>
<!-- Opening Hours, day permitted values: 1(Monday) - 7(Sunday) and 8 (workweek) -->
<openingTime day = "1">7:00 - 13:00 15:00 - 18:00</openingTime>
<openingTime day = "2">7:00 - 13:00 15:00 - 18:00</openingTime>
<openingTime day = "3">7:00 - 13:00 15:00 - 18:00</openingTime>
<openingTime day = "4">7:00 - 13:00 15:00 - 18:00</openingTime>
<openingTime day = "5">7:00 - 13:00 15:00 - 18:00</openingTime>
</openingHours>
</store>
</stores>
这是我的java代码:
NodeList hoursList = storeElement.getElementsByTagName("openingHours").item(0).getChildNodes();
for (int y = 0; y < hoursList.getLength(); y++) {
Node hoursNode = hoursList.item(y);
boolean isElement = hoursNode.getNodeType() == Node.ELEMENT_NODE;
boolean hasChild = hoursNode.getChildNodes().getLength() != 0;
String nameParent = hoursNode.getParentNode().getNodeName();
boolean isElement为false,hasChild也为false,String nameParent为openingHours。