标签:style c class blog code java
一、dom4j
import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element;
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); } }
二、jdom
import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.input.SAXBuilder; import org.jdom2.input.sax.XMLReaderSAX2Factory;
参数为File类型的xml
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()); }
参数为String类型的xml 做String到InputStream的转换
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();//获得根节点
标签:style c class blog code java
原文地址:http://www.cnblogs.com/helloxiaoxiang/p/3754650.html