• dom4j+xpath解析带有属性的xml文档


    server.xml

    <?xml version="1.0" encoding="utf-8"?>
    <server>
        <service>
            <connector port ="8080"></connector>
        </service>
    </server>
    package edu.aeon.xml;
    
    import org.dom4j.Attribute;
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;
    
    /**
     * [说明]:通过dom4j+xpath获取节点属性的值
     *     需要导入dom4j和xpath的jar包
     * @author aeon(qq:1584875179)
     *
     */
    public class Dom4jXpathAttri {
    
        public static void main(String[] args) {
            try {
                SAXReader saxReader=new SAXReader();
                Document document=saxReader.read("config/server.xml");
                Element connectorElement = (Element) document.selectSingleNode("/server/service/connector");
                //2种方式任选一种即可
                Attribute attribute = connectorElement.attribute("port");
                String attributeValue1=attribute.getStringValue();
                String attributeValue2 = connectorElement.attributeValue("port");
                System.out.println(attributeValue1+"	"+attributeValue2);
            } catch (DocumentException e) {
                e.printStackTrace();
            }
        }
    }

    结果截图:

      

    如有任何疑问可联系邮箱: 给我发邮件、或直接联系QQ:1584875179 || 点返回首页

  • 相关阅读:
    习题4.7利用vector实现数据复制
    习题4.18
    4.8编写一小段程序实现两vector是否相等的比较
    关于野指针
    学习c++的50条忠告
    c++头文件
    习题4.14
    容器和迭代器
    Android上C++对象的自动回收机制分析
    Windows下载Android源码
  • 原文地址:https://www.cnblogs.com/aeon/p/10766170.html
Copyright © 2020-2023  润新知