dom4j解析xml示例

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用

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>


标签: ssl

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:C#的SQLiteHelper类似SqlHelper类实现存取Sqlite数据库

下一篇:C#实现中文转拼音的类