码迷,mamicode.com
首页 > 其他好文 > 详细

解析XML

时间:2014-05-28 23:05:00      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:style   c   class   blog   code   java   

一、dom4j

bubuko.com,布布扣
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
bubuko.com,布布扣
bubuko.com,布布扣
            Document document = DocumentHelper.parseText(xmltojopo); //document 
            Element root = document.getRootElement();
             
            for(Iterator i = root.elementIterator(); i.hasNext(); ){//遍历Info
                Element infos = (Element) i.next();   
if(infos.getName().equals("caseInfo")){ for(Iterator j = infos.elementIterator(); j.hasNext();){//遍历元素 Element elememtOfInfo = (Element) j.next(); //得到属性 String elementOfInfoName = elememtOfInfo.getName();//得到属性的类型 String elementOfInfoValue = elememtOfInfo.getText();//得到属性的值
            caseInfoMap.put(elementOfInfoName, elementOfInfoValue); } }
bubuko.com,布布扣

二、jdom

bubuko.com,布布扣
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.input.sax.XMLReaderSAX2Factory;
bubuko.com,布布扣


参数为File类型的xml

bubuko.com,布布扣
 InputStream xmlFileStream = null;
 xmlFileStream = new FileInputStream(xmlFile);
 SAXBuilder builder=new SAXBuilder(new XMLReaderSAX2Factory(false, null));
 Document document = builder.build(xmlFileStream);//获得文档对象
 Element root = document.getRootElement();//获得根节点
 Element consignElement = root.getChild("caseInfo");
 List<Element> elements = caseInfoElement.getChildren();
 for(Element ele : elements){
    System.out.println(ele.getName());
    System.out.println(ele.getTextTrim());
    Field field = objectClz.getDeclaredField(chiledEle.getName());
    field.setAccessible(true);
    System.out.println(field.getType().getName());
 }
bubuko.com,布布扣

参数为String类型的xml 做String到InputStream的转换

bubuko.com,布布扣
InputStream xmlStream = null;
xmlStream
= new ByteArrayInputStream(dataInfo.getBytes("utf-8"));
SAXBuilder builder
=new SAXBuilder(new XMLReaderSAX2Factory(false, null)); Document document = builder.build(xmlStream);//获得文档对象
Element root = document.getRootElement();//获得根节点
bubuko.com,布布扣

 

 

解析XML,布布扣,bubuko.com

解析XML

标签:style   c   class   blog   code   java   

原文地址:http://www.cnblogs.com/helloxiaoxiang/p/3754650.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!