• 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 || 点返回首页

  • 相关阅读:
    LeetCode
    在linux服务器下部署python工程(爬虫)
    linux安装python3.6 及 beautifulsoup
    HDU 1561 The more, The Better[树形dp/01背包]
    POJ 3107 Godfather[树的重心]
    POJ 1655 Balancing Act[树的重心/树形dp]
    HDU 2169 Computer[树形dp]
    HDU
    POJ1721--CARDS [置换]
    POJ 1026 Cipher[置换]
  • 原文地址:https://www.cnblogs.com/aeon/p/10766170.html
Copyright © 2020-2023  润新知