如何使用DOM解析n级xml

如何使用DOM解析n级xml,xml,dom,xml-parsing,Xml,Dom,Xml Parsing,我需要解析一个n级xml文件并显示其元素。它永远不会有任何属性。 我当前的代码 String xmlInputFile="reportA.xml" ; File file =new File(xmlInputFile); Document document; DocumentBuilder documentBuilder; DocumentBuilderFactory documentBuilderFactory;

我需要解析一个n级xml文件并显示其元素。它永远不会有任何属性。 我当前的代码

String xmlInputFile="reportA.xml"  ;
        File file =new File(xmlInputFile);
        Document document;
        DocumentBuilder documentBuilder;
        DocumentBuilderFactory documentBuilderFactory;
        NodeList nodeList;
        documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilder = documentBuilderFactory.newDocumentBuilder();
        document = documentBuilder.parse(xmlInputFile);

        document.getDocumentElement().normalize();
         nodeList = document.getElementsByTagName("*");
        for (int index = 0; index < nodeList.getLength(); index++) {
            Node nodeA = nodeList.item(index);
            if (nodeA.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) nodeA;
                if(element.hasChildNodes()){
                    System.out.println("Name "+element.getNodeName()+" value "+element.getFirstChild().getNodeValue().trim());
                }
            }
        }
我不需要公司,记录,名字。如何删除这些元素?

将代码更改为

nodeList = document.getElementsByTagName("*");
        for (int index = 0; index < nodeList.getLength(); index++) {
            Node nodeA = nodeList.item(index);
           if (nodeA.getNodeType() == Node.ELEMENT_NODE) {
               NodeList nodeList1 = nodeA.getChildNodes();
               if(nodeList1.getLength()<=1 ){
                   String value="";
                    if(nodeList1.getLength()!=0){
                        value= nodeA.getFirstChild().getNodeValue();
                    } 
                    System.out.println("Name "+nodeA.getNodeName()+" value "+ value);
               }
           }

        }
nodeList=document.getElementsByTagName(“*”);
对于(int index=0;indexName company value 
Name record value 
Name name value 
Name firstName value Brad
Name lastName value Pitt
Name age value 41
Name dob value 31/8/1982
Name income value 200,000
nodeList = document.getElementsByTagName("*");
        for (int index = 0; index < nodeList.getLength(); index++) {
            Node nodeA = nodeList.item(index);
           if (nodeA.getNodeType() == Node.ELEMENT_NODE) {
               NodeList nodeList1 = nodeA.getChildNodes();
               if(nodeList1.getLength()<=1 ){
                   String value="";
                    if(nodeList1.getLength()!=0){
                        value= nodeA.getFirstChild().getNodeValue();
                    } 
                    System.out.println("Name "+nodeA.getNodeName()+" value "+ value);
               }
           }

        }