ccc9149
未知昵称
2019-06-13 13:30
采纳率: 89.7%
浏览 480
已采纳

java 用dom的方式解析xml文件,子节点下有相同名字的子节点?

如下图所示第一级含有1,2,3,4 等多个节点,
1下面含有1.1,1.2,1.3,1.4,1.5 等多个节点,
1.2下面含有1.2.1,1.2.2 等多个节点,
解析代码如下:
标注的此处有问题的,testSublist的长度,包括了1.1到1.5和1.2.1到1.2.2,也就是也就是统计出了1下面所有出现了IMPLEMENTATION-DATA-TYPE-ELEMENT,现在我只希望,获得的长度为5,也就是1.1到1.5,也就是一级一级获取下去??

NodeList testList = doc.getElementsByTagName("IMPLEMENTATION-DATA-TYPE");
for(int i = 0; i < impList.getLength(); i++) {
            Element testElement = (Element) testList.item(i);   
            NodeList testSublist=testElement.getElementsByTagName("IMPLEMENTATION-DATA-TYPE-ELEMENT"); //此处有问题
for (int ii = 0; ii < testSublist.getLength(); ii++) {
}
}

图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • liujianchen_linux
    Next66 2019-06-13 14:44
    已采纳

    兄弟,还在纠结这个问题呀;
    自己实现个getChildrenByTagName,再结合我上次给你的代码
    看下面代码:

        public static List<Node> getChildrenByTagName(Node nl, String name) {
            List<Node> nodeList = new ArrayList<Node>();
            for (Node child = nl.getFirstChild(); child != null; child = child.getNextSibling()) {
                if(child.getNodeType() == Node.ELEMENT_NODE && 
                    name.equals(child.getNodeName())){
                    nodeList.add(child);
                }
            }
            return nodeList;
        }
    
            public static void main(String[] args) {
    
            DocumentBuilderFactory docbf = DocumentBuilderFactory.newInstance();
            try {
                DocumentBuilder docb = docbf.newDocumentBuilder();
                Document doc = docb.parse("test.xml");
                List<Node> elements = getChildrenByTagName(doc, "IMPLEMENTATION-DATA-TYPE");
                System.out.println(elements);
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    点赞 评论
  • dabocaiqq
    dabocaiqq 2019-06-13 14:01
    package com.test;
    
    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    
    public class DomXML {
    
        public static void main(String[] args) {
            try {
                File file = new File("e:/People.xml");  
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();   
                DocumentBuilder builder = factory.newDocumentBuilder();   
                Document document = builder.parse(file);   
                Element element = document.getDocumentElement();
    
                List<People> peopleList = new ArrayList<People>();
                NodeList peopleNodes = element.getElementsByTagName("People"); 
                for(int i=0;i<peopleNodes.getLength();i++){   
                    People people = new People();
                    Element peopleElement = (Element) peopleNodes.item(i);
                    people.setId(peopleElement.getAttribute("id"));
                    NodeList childPeopleNodes = peopleElement.getChildNodes();
                    for(int j=0;j<childPeopleNodes.getLength();j++){
                        //DOM解析时候注意子节点前面的空格也会被解析
                        if(childPeopleNodes.item(j) instanceof Element){
                            Element childPeopleElement = (Element) childPeopleNodes.item(j);
                            if(childPeopleElement.getNodeType()==Node.ELEMENT_NODE){  
                                if(childPeopleElement.getNodeName().equals("Name")){
                                    people.setEnglishName(childPeopleElement.getAttribute("en"));
                                    people.setName(childPeopleElement.getTextContent());
                                }
                                else if(childPeopleElement.getNodeName().equals("Age")){
                                    people.setAge(childPeopleElement.getTextContent());    
                                }
                            }
                        }
                    }
                    peopleList.add(people);
                }
    
                for(People people : peopleList){
                    System.out.println(people.getId()+"\t"+people.getName()+"\t"+people.getEnglishName()+"\t"+people.getAge());
                }
    
            } catch (Exception e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            } 
    
    
        }
    }
    
    点赞 评论
  • tiankongzhichenglyf
    tkzc_shark 2019-06-13 14:36

    使用getChildNodes

            NodeList testList = doc.getElementsByTagName("IMPLEMENTATION-DATA-TYPE");
            for (int i = 0; i < impList.getLength(); i++) {
                Element testElement = (Element) testList.item(i);
                NodeList testSublist = testElement.getChildNodes();
                for (int ii = 0; ii < testSublist.getLength(); ii++) {
                }
            }
    
    点赞 评论

相关推荐