dom4j解析xml示例

bubblue 8年前發布 | 904 次閱讀 Java Dom4j

java xml解析框架dom4j解析xml示例

 

public class Dom4JTest {

    private static Dom4JTest dom4JTest=null;
    private static Document document = null;

    static{
          try {
              InputStream inputStream = Dom4JTest.class.getClassLoader().getResourceAsStream("cp-info.xml");
              // 讀取輸入流
              SAXReader reader = new SAXReader();
              document = reader.read(inputStream);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }


    private Dom4JTest() {
    }
    public static Dom4JTest getInstance(){
        if(dom4JTest==null){
            dom4JTest = new Dom4JTest();
        }
        return dom4JTest;
    }



    public  List<Map<String,String>> getCpInfoByCpName(String cpName) throws Exception{
        List<Map<String,String>> returnList = new ArrayList<Map<String,String>>();
        // 得到xml根元素
        Element root = document.getRootElement();  
        List cpelements = root.elements("cp");
        for(Element e:cpelements){
            String name = e.attribute("name").getValue();
            if(cpName.equals(name)){
                List itemElement = e.element("items").elements("item");
                for(Element et:itemElement){
                    String title =et.element("title").getTextTrim();
                    String picurl = et.element("picurl").getTextTrim();
                    String url = et.element("url").getTextTrim();
                    
                    Map<String,String> map = new HashMap<String,String>();
                    map.put("title", title);
                    map.put("picurl", picurl);
                    map.put("url", url);
                    returnList.add(map);
                }
            }
        }
        return returnList;
    }


    public static void main(String[] args) throws Exception {

        Dom4JTest instance = Dom4JTest.getInstance();
        List<Map<String, String>> cpInfoByCpName = instance.getCpInfoByCpName("借貸寶");
        for(Map<String, String> m:cpInfoByCpName){
            System.out.println(m.get("title"));
            System.out.println(m.get("picurl"));
            System.out.println(m.get("url"));
        }
    }

}


<?xml version="1.0" encoding="UTF-8"?>
<all>
    <cp name="name-1">
        <items>
            <item>
                <title>
                    title-測試
                </title>
                <description>
                </description>
                <picurl>
                    http://test/img/big.png
                </picurl>
                <url>
                    <![CDATA[
                     http://test/s?&idx=1&sn=49f13b923649b6d0098c3aadbf715f2d#rd
                    ]]>
                </url>
            </item>
            <item>
                <title>
                    title-測試-2
                </title>
                <description>
                </description>
                <picurl>
                    http://test/img/big-2.png
                </picurl>
                <url>
                    <![CDATA[
                     http://test/s?&idx=2&sn=49f13b923649b6d0098c3aadbf715f2d#rd
                    ]]>
                </url>
            </item>
        </items>
    </cp>
    <cp name="name-2">
        <items>
            <item>
                <title>
                    title-測試
                </title>
                <description>
                </description>
                <picurl>
                    http://test/img/big-2.png
                </picurl>
                <url>
                    <![CDATA[
                      http://test/s?&idx=1&sn=49f13b923649b6d0098c3aadbf715f2d#rd
                    ]]>
                </url>
            </item>
        </items>
    </cp>
<all>


 本文由用戶 bubblue 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!