首页 > 代码库 > dom4j处理java中xml还是很方便的
dom4j处理java中xml还是很方便的
http://blog.csdn.net/chenghui0317/article/details/11486271
输入:
String flighter = RequestUtil.get("http://flash.weather.com.cn/wmaps/xml/china.xml");
Document document = DocumentHelper.parseText(flighter);
Element rootElement = document.getRootElement();
List<Element> elementList=rootElement.elements();
Element elm;
for(int i=0;i<elementList.size();i++)
{
elm = elementList.get(i);
System.out.println(elm);
//System.out.println(elm.elementTextTrim("city"));//获取节点文本值
System.out.println(elm.attributeValue("quName"));//attributeValue获取节点属性值
}
输出:
Document doc = null;
doc = DocumentHelper.createDocument();
doc.setXMLEncoding("GB2312");
Element res=doc.addElement("result");
Element resultInfo = res.addElement("resultInfo");
resultInfo.addAttribute("result", "failure");
resultInfo.addAttribute("error_code", "400");
resultInfo.addAttribute("error_reason", "验证失败");
resultInfo.addAttribute("error_type", "");
Element ser = res.addElement("service");
ser.addAttribute("business", "ITV-CMS-MA");
ser.addAttribute("function", "addMedia");
output(doc.asXML());
dom4j处理java中xml还是很方便的