【问题标题】:How to parse the following xml using DOM parser?如何使用 DOM 解析器解析以下 xml?
【发布时间】:2014-09-12 10:10:14
【问题描述】:

如何使用 DOM 解析器解析以下 xml - name 节点重复到 n 级

<Services>
    <service name ="qwerty" id="">
    <File rootProfile="abcd" extension="acd">
    <Columns>
    <name id="0" profileName="DATE" type="java"></name>
    <name id="1" profileName="DATE" type="java"></name>
    .
    .
    .
    <Columns>
    </File>
    <File rootProfile="efg" extension="ghi">
    <Columns>
    <name id="a" profileName="DATE" type="java"></name>
    <name id="b" profileName="DATE" type="java"></name>
    .
    .
    .
    <Columns>
    </File>
    </service>
    </Services>

我使用以下代码获取值:

public static void xmlEditor() throws SAXException, IOException {
    try {
        File xmlFile = new File("config/ServiceConfig.xml");
        DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = null;

        documentBuilder = documentFactory.newDocumentBuilder();

        org.w3c.dom.Document doc = documentBuilder.parse(xmlFile);
        doc.getDocumentElement().normalize();
        NodeList nodeList0 = doc.getElementsByTagName("Service");
        NodeList nodeList1 = doc.getElementsByTagName("File");
        NodeList nodeList2 = doc.getElementsByTagName("name");
        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

        for (int temp0 = 0; temp0 < nodeList0.getLength(); temp0++) {
            Node node0 = nodeList0.item(temp0);

            System.out.println("\nElement type :" + node0.getNodeName());
            Element Service = (Element) node0;

            if (node0.getNodeType() == Node.ELEMENT_NODE) {
                System.out.println("-----------------" + temp0 + "----------------------------------");
                System.out.println("name : " + Service.getAttribute("name"));
                System.out.println("id : " + Service.getAttribute("id"));


                for (int temp = 0; temp < nodeList1.getLength(); temp++) {
                    Node node1 = nodeList1.item(temp);

                    System.out.println("\nElement type :" + node1.getNodeName());

                    Element File = (Element) node1;

                    if (node1.getNodeType() == Node.ELEMENT_NODE) {

                        System.out.println("rootProfile:" + File.getAttribute("rootProfile"));
                        System.out.println("extension  : " + File.getAttribute("extension"));


                        for (int temp1 = 0; temp1 < nodeList2.getLength(); temp1++) {
                            Node node2 = nodeList2.item(temp1);

                            System.out.println("\nElement type :" + node2.getNodeName());

                            Element name = (Element) node2;

                            if (node2.getNodeType() == Node.ELEMENT_NODE) {

                                System.out.println("id:" + name.getAttribute("id"));
                                System.out.println("profileName  : " + name.getAttribute("profileName"));
                                System.out.println("type  : " + name.getAttribute("type"));

                            }

                        }
                    }

                }
            }

        }

    }
}

并且正在获取第一级节点 File 本身正在获取 name 的所有值,包括下一个文件节点有助于避免这种情况 我得到的输出是

name:-----
id  :-----

rootProfile:--------
 extension:-----------

id:o
profileName:
type:
id:1
profileName:
type:
id:a
profileName:
type:
id:b
profileName:
rootProfile:--------
 extension:-----------

id:o
profileName:
type:
id:1
profileName:
type:
id:a
profileName:
type:
id:b
profileName:

【问题讨论】:

    标签: java xml parsing dom


    【解决方案1】:

    已发布完整示例。您可能需要从 File 而不是我使用的 String 中读取 XML。

        import java.io.ByteArrayInputStream;
    import java.io.IOException;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.InputSource;
    import org.xml.sax.SAXException;
    
    public class MyTester {
    
        public static void main(String[] args) throws IOException, SAXException,
                ParserConfigurationException {
            xmlEditor();
        }
    
        public static void xmlEditor() throws SAXException, IOException,
                ParserConfigurationException {
            // File xmlFile = new File("ServiceConfig.xml");
            String xml = "<Services><service name=\"qwerty\" id=\"\"><File rootProfile=\"abcd\" extension=\"acd\"><Columns>"
                    + "<name id=\"0\" profileName=\"DATE\" type=\"java\"></name><name id=\"1\" profileName=\"DATE\" type=\"java\"></name>"
                    + "</Columns></File><File rootProfile=\"efg\" extension=\"ghi\"><Columns><name id=\"a\" profileName=\"DATE\" type=\"java\"></name>"
                    + "<name id=\"b\" profileName=\"DATE\" type=\"java\"></name></Columns></File></service></Services>";
            DocumentBuilderFactory documentFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder documentBuilder = null;
    
            documentBuilder = documentFactory.newDocumentBuilder();
    
            // org.w3c.dom.Document doc = documentBuilder.parse(xmlFile);
            org.w3c.dom.Document doc = documentBuilder.parse(new InputSource(
                    new ByteArrayInputStream(xml.getBytes())));
    
            doc.getDocumentElement().normalize();
            NodeList nodeList0 = doc.getElementsByTagName("service");
            NodeList nodeList1 = null;
            NodeList nodeList2 = null;
            System.out.println("Root element :"
                    + doc.getDocumentElement().getNodeName());
    
            for (int temp0 = 0; temp0 < nodeList0.getLength(); temp0++) {
                Node node0 = nodeList0.item(temp0);
    
                System.out.println("\nElement type :" + node0.getNodeName());
                Element Service = (Element) node0;
    
                if (node0.getNodeType() == Node.ELEMENT_NODE) {
                    System.out.println("-----------------" + temp0
                            + "----------------------------------");
                    System.out.println("name : " + Service.getAttribute("name"));
                    System.out.println("id : " + Service.getAttribute("id"));
                    nodeList1 = Service.getChildNodes();
                    for (int temp = 0; temp < nodeList1.getLength(); temp++) {
                        Node node1 = nodeList1.item(temp);
    
                        System.out
                                .println("\nElement type :" + node1.getNodeName());
    
                        Element File = (Element) node1;
    
                        if (node1.getNodeType() == Node.ELEMENT_NODE) {
    
                            System.out.println("rootProfile:"
                                    + File.getAttribute("rootProfile"));
                            System.out.println("extension  : "
                                    + File.getAttribute("extension"));
    
                            nodeList2 = File.getChildNodes();// colums
                            for (int temp1 = 0; temp1 < nodeList2.getLength(); temp1++) {
                                Element column = (Element) nodeList2.item(temp1);
                                NodeList nodeList4 = column.getChildNodes();
                                for (int temp3 = 0; temp3 < nodeList4.getLength(); temp3++) {
                                    Element name = (Element) nodeList4.item(temp3);
                                    if (name.getNodeType() == Node.ELEMENT_NODE) {
    
                                        System.out.println("id:"
                                                + name.getAttribute("id"));
                                        System.out.println("profileName  : "
                                                + name.getAttribute("profileName"));
                                        System.out.println("type  : "
                                                + name.getAttribute("type"));
    
                                    }
                                }
                            }
    
                        }
    
                    }
                }
    
            }
        }
    
    }
    

    输出:

    根元素:服务

    元素类型:服务 -----------------0-------------------------------- -- 名称:qwerty id:

    元素类型:文件根配置文件:abcd 扩展名:acd id:0 profileName : 日期类型 : java id:1 profileName : 日期类型 : java

    元素类型:文件根配置文件:efg 扩展名:ghi id:a profileName : 日期类型 : java id:b profileName : 日期类型 : java

    【讨论】:

      猜你喜欢
      • 2013-01-31
      • 1970-01-01
      • 2013-07-18
      • 1970-01-01
      • 2013-12-24
      • 2012-02-26
      • 1970-01-01
      • 2011-05-09
      • 1970-01-01
      相关资源
      最近更新 更多