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

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>


你可能感兴趣的:(java xml解析框架dom4j解析xml示例)