Вот используемые данные. < /p>
Код: Выделить всё
North East
Full
Empty
Есть 3 вопроса, встроенные в код в разных местах, где меня, кажется, споткнулись. < /p>
import java.io.File;
import java.io.IOException;
//from w ww. j a v a 2 s . co m
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;
public class XMLFromjava2
{
public static void main(String[] args) throws IOException, ParserConfigurationException, org.xml.sax.SAXException
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringComments(true);
factory.setCoalescing(true);
factory.setNamespaceAware(false);
factory.setValidating(false);
DocumentBuilder parser = factory.newDocumentBuilder();
Document document = parser.parse(new File("C:\\Downloads\\DummyData2.xml"));
NodeList locations = document.getElementsByTagName("DeliveryLocations"); ///Starting point is the tag
int numLocations = locations.getLength();
System.out.println("There are " + numLocations + " locations.");
for (int i = 0; i < numLocations; i++) ///Iterates through all DeliveryLocations tag (Only 1 in this example.)
{
Element section = (Element) locations.item(i);
System.out.println("Node : " + section.getNodeName()); ///Prints out the Location Tag.
Node textNode = section.getFirstChild(); ////First Child is the Text Node which is empty (not NULL)
if (textNode.getNodeValue() != null && textNode.getNodeValue().trim().length() > 0) ////QUESTION 1 : Why is the Text Node empty and not null?????
{
System.out.println(" Text Node : " + textNode.getNodeValue()); ///If there was text for the tag it would appear here.
}
else
System.out.println(" Text Node : or "); ///This is what is printed
////Going into the LOOP as a Text Node.
while (textNode != null && textNode.getNodeType() != Node.ELEMENT_NODE)
{
System.out.println (" Before getNextSibling() NodeType : " + textNode.getNodeType()); ////Text Node
textNode = textNode.getNextSibling(); /////QUESTION 2 : Why did getNextSibling() work shouldn't it require getNextChild()?????
System.out.println (" After getNextSibling() NodeType : " + textNode.getNodeType()); ///Element Node
System.out.println(" Text Node : " + textNode.getNodeName());
}
if (textNode != null)
{
System.out.println("Data : " + textNode.getFirstChild().getNodeValue()); /////The firstChild is the Text Node and that value
////is the actual data "North East"
System.out.println("Confirm which Node : " + textNode.getNodeName()); ///This confirms still on Location Node.
System.out.println("2nd print : " + textNode.getNextSibling().getNodeName()); ///QUESTION 3 : Why does this get a Text Node???
///Wouldn't a getChild() get the Text node
///If the Node is Location then shouldn't its Sibling be Item1??
}
}
}
}
< /code>
Из приведенных выше вопросов это относится к областям, если бы я ожидал, а не GetChild и наоборот. < /p>
Вы можете помочь уточнить путаницу?
Подробнее здесь: https://stackoverflow.com/questions/684 ... tchildnode
Мобильная версия