ccc9149
未知昵称
采纳率89.7%
2019-06-10 14:42

java 用dom4j解析xml文件中的制定标签节点下的内容

20
已采纳

如下图所示,P-PORT-PROTOTYPE 为xml文件中的节点,在任意位置,

用dom4j去解析该xml文件,如何直接定位到此节点,获取到xml文件中有多少个P-PORT-PROTOTYPE节点,然后ORT-PROTOTYPE下的SHORT-NAME的值

图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • qq_37731391 找一个女朋友 2年前
     private static String  xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<root>\n" +
                "\t<root1>\n" +
                "\t\t<name>\n" +
                "\t\t\t<P-PORT-PROTOTYPE>\n" +
                "\t\t\t\t <SHORT-NAME>NAME1</SHORT-NAME>\n" +
                "\t\t\t\t <PROVIDED-INTERFACE-TREF>TREF1</PROVIDED-INTERFACE-TREF>\n" +
                "\t\t\t</P-PORT-PROTOTYPE>\n" +
                "\t\t</name>\n" +
                "\t\t<ports>\n" +
                "\t\t\t<P-PORT-PROTOTYPE>\n" +
                "\t\t\t\t <SHORT-NAME>NAME1</SHORT-NAME>\n" +
                "\t\t\t\t <PROVIDED-INTERFACE-TREF>TREF1</PROVIDED-INTERFACE-TREF>\n" +
                "\t\t\t</P-PORT-PROTOTYPE>\n" +
                "\t\t\t<P-PORT-PROTOTYPE>\n" +
                "\t\t\t\t<SHORT-NAME>NAME2</SHORT-NAME>\n" +
                "\t\t\t\t<PROVIDED-INTERFACE-TREF>TREF2</PROVIDED-INTERFACE-TREF>\n" +
                "\t\t\t</P-PORT-PROTOTYPE>\n" +
                "\t\t\t<P-PORT-PROTOTYPE>\n" +
                "\t\t\t\t<SHORT-NAME>NAME3</SHORT-NAME>\n" +
                "\t\t\t\t<PROVIDED-INTERFACE-TREF>TREF3</PROVIDED-INTERFACE-TREF>\n" +
                "\t\t\t</P-PORT-PROTOTYPE>\n" +
                "\t\t\t<P-PORT-PROTOTYPE>\n" +
                "\t\t\t\t<SHORT-NAME>NAME4</SHORT-NAME>\n" +
                "\t\t\t\t<PROVIDED-INTERFACE-TREF>TREF4</PROVIDED-INTERFACE-TREF>\n" +
                "\t\t\t</P-PORT-PROTOTYPE>\n" +
                "\t\t</ports>\n" +
                "\t</root1>\n" +
                "</root>";
    
        public static void main(String[] args) throws Exception{
            //将xml字符串转为Document对象
            Document document = DocumentHelper.parseText(xmlStr);
            //获取根节点 root节点
            Element rootElement = document.getRootElement();
            Map<String,Element> map = new HashMap<String,Element>();
            List<String> list = new ArrayList<String>();
            getE("P-PORT-PROTOTYPE",rootElement,map);
            System.out.println("匹配上"+map.size()+"个P-PORT-PROTOTYPE节点信息");
            for (String s : map.keySet()){
                System.out.println(s + "节点路径下的信息如下:");
                List<Element> elements = map.get(s).getParent().elements("P-PORT-PROTOTYPE");
                System.out.println("P-PORT-PROTOTYPE节点个数为:"+elements.size());
                for(Element e : elements){
                    for(Element e1 : e.elements()){
                        System.out.println(e1.getName() + "========" + e1.getTextTrim());
                    }
                }
            }
    
        }
    
        /**
         * 匹配指定元素
         * @param EName 目标元素
         * @param element 根元素
         * @param map 存放匹配上的元素集合
         */
        public static void getE(String EName, Element element, Map<String,Element> map){
           if(element.hasMixedContent() && !element.getName().equals(EName)){
               List<Element> elements = element.elements();
               for(Element e : elements){
                   getE(EName,e,map);
               }
           }else if (element.getName().equals(EName)){
               map.put(element.getPath(),element);
           }
        }
    
    点赞 评论 复制链接分享
  • dabocaiqq dabocaiqq 2年前
    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();
            } 
    
    
        }
    }
    
    点赞 2 评论 复制链接分享
  • qq_41126602 qq_41126602 2年前

    Document document = reader.read(file);
    Element root = document.getRootElement();
    List childElements = root.elements();
    for (Element child : childElements) {
    //循环数据
    List attributeList = child.attributes();
    for (Attribute attr : attributeList) {
    System.out.println(attr.getName() + ": " + attr.getValue());
    }

            //直接 获取属性中的值
            System.out.println("id: " + child.attributeValue("id"));
    
    点赞 评论 复制链接分享

相关推荐