标签:puts style ext stack height public 代码 put source
/** * 02 * 使用此种方式获得InputStream, * xml目录只能在src下(可以在src下新建文件夹); */ @Test public void testParseXml02() { InputStream in = ParseXml.class.getClassLoader().getResourceAsStream("config"+File.separator+"testxml.xml"); try { SAXReader reader = new SAXReader(); Document doc = reader.read(in);
//获得根标签 Element root = doc.getRootElement(); Element em ;
//value 为二级标签,name和password为value内标签 for(Iterator<?> i = root.elementIterator("value"); i.hasNext();){ em = (Element) i.next(); System.out.println(em.elementText("name")); System.out.println(em.elementText("password")); } } catch (DocumentException e) { e.printStackTrace(); } }
/** * 02 * 使用此种方式解析 * 使用 new File */ @Test public void testParseXml01() throws FileNotFoundException { // System.getProperty("user.dir") 是动态获得当前文件的路径 File file = new File(System.getProperty("user.dir")+File.separator+"xmlOfConfig"+File.separator+"testxml.xml"); try { SAXReader reader = new SAXReader(); Document doc = reader.read(file); Element root = doc.getRootElement(); Element em ; for(Iterator<?> i = root.elementIterator("value"); i.hasNext();){ em = (Element) i.next(); System.out.println(em.elementText("name")); System.out.println(em.elementText("password")); } } catch (DocumentException e) { e.printStackTrace(); } }
<?xml version=‘1.0‘ encoding=‘utf-8‘?> <document> <name>qq</name> <password>123</password> </document>
小菜鸟一枚,以上代码有问题之处欢迎指正,学习ing。
标签:puts style ext stack height public 代码 put source
原文地址:http://www.cnblogs.com/moly/p/6841324.html