Java DOM XML cannot get child
•
Java
My XML looks like this:
<ConnProf ConnProfID="1111"> <ConnNum>1</ConnNum> <IsMSPA>false</IsMSPA> <IsArray>false</IsArray> <IsDDOR>false</IsDDOR> <Subsystem SSID="2222"ConnProfID="3333"> <SSName>AA</SSName> <GenericSSName>AA</GenericSSName> <ConnFuncAddr>aaa</ConnFuncAddr> <DSSNum>22</DSSNum> <isRemoved>false</isRemoved> </Subsystem> <Subsystem SSID="4444" ConnProfID="5555"> <SSName>BBBB</SSName> <GenericSSName>BB</GenericSSName> <ConnFuncAddr>bbbbbb</ConnFuncAddr> <DSSNum>44</DSSNum> <isRemoved>false</isRemoved> </Subsystem>
I can't get connnum, ismspa, isarray and isddor I tried using connnum:
//get ConnNum Node n = doc.getFirstChild(); if (n.hasChildNodes()) System.out.println(n.getFirstChild().getNodeValue()); else System.out.println(n.getNodeValue());
But when I expect 1, it only returns null
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 test { public static void main(String[] args) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { DocumentBuilder db = dbf.newDocumentBuilder(); for (int i = 1; i <= 8; i++) { Document doc = db.parse("file" + i + ".xml"); doc.getDocumentElement ().normalize (); System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName()); //get ConnNum Node n = doc.getFirstChild(); if (n.hasChildNodes()) System.out.println(n.getFirstChild().getNodeValue()); else System.out.println(n.getNodeValue()); NodeList listOfSubsystems = doc.getElementsByTagName("Subsystem"); int totalSubsystems = listOfSubsystems.getLength(); if (totalSubsystems == 0) continue; else { System.out.println("Total number of subsystems : " + totalSubsystems + "\n"); Dish dish = new Dish(); for(int s=0; s < listOfSubsystems.getLength() ; s++) { Node firstPersonNode = listOfSubsystems.item(s); if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE) { Element firstPersonElement = (Element)firstPersonNode; printElement(firstPersonElement,"SSName"); printElement(firstPersonElement,"GenericSSName"); printElement(firstPersonElement,"ConnFuncAddr"); printElement(firstPersonElement,"DSSNum"); printElement(firstPersonElement,"SCNum"); printElement(firstPersonElement,"SCAcronym"); printElement(firstPersonElement,"PassNum"); printElement(firstPersonElement,"FzCode"); printElement(firstPersonElement,"isRemoved"); System.out.println("------------------"); } } System.out.println("\n=============================="); } } } catch(ParserConfigurationException pce) { pce.printStackTrace(); } catch(SAXException se) { se.printStackTrace(); } catch(IOException ioe) { ioe.printStackTrace(); } } public static void printElement(Element a,String name) { NodeList elementList = a.getElementsByTagName(name); Element b = (Element)elementList.item(0); if (b != null) { NodeList list = b.getChildNodes(); System.out.println( ((Node)list.item(0)).getNodeValue().trim() ); } } }
Solution
Maybe the first child is not what you think Whitespace is important in XML, and firstchild may actually be a text node
Nodes has a type. You can iterate over all child nodes that check the element node type to get the handle of the actual element
Edit: this will print your subsequent values It filters on the element node, and then filters the first child node of each node (the node containing text)
NodeList nodeList = n.getChildNodes(); for (int j = 0; j < nodeList.getLength(); j++) { Node childNode = nodeList.item(j); if (childNode.getNodeType() == Node.ELEMENT_NODE) { System.out.println(childNode.getNodeName() + " " + childNode.getFirstChild().getNodeValue()); } }
In addition, as @ Steve Townsend wrote correctly, if you are using java 1.5 or later, you can use gettextcontent () instead of childnode getFirstChild(). getNodeValue().
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码