使用dom4j解析xml字符串

package dom4j;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* 使用dom4j处理xml字符串;使用迭代器可以搞定一切;
* 附件是转成base64的字符串;便于webservice基于soap的传输;
* @author shuaisweet
*/
public class InfoXML
{
public void doInfoXML(String infoXML)
{
System.out.println(infoXML);
SAXReader reader = new SAXReader();
Document document;
try
{
document = DocumentHelper.parseText(infoXML);
Element root = document.getRootElement();
//信息条数;
int info_count=root.elements().size();
Iterator it =root.elements().iterator();
while(it.hasNext())
{
Element info=(Element) it.next();
System.out.println(info.elementText("info_title"));
System.out.println(info.elementText("index_code"));
System.out.println(info.elementText("content"));
//在此处理信息
System.out.println("附件开始");
Element adjuncts= info.element("adjuncts");
Iterator adjs=adjuncts.elements().iterator();
while(adjs.hasNext())
{
Element adj=(Element) adjs.next();
System.out.println(adj.elementText("file_name"));
System.out.println(adj.elementText("adjunct_content"));
//在此处理附件;
}
System.out.println("==============================");
}
} catch (DocumentException e1)
{
e1.printStackTrace();
}
}
public static void main(String[] args)
{
InfoXML xml=new InfoXML();
String infoXML="<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
"<infos>"+
"<info>"+
"<info_title><![CDATA[aaaaaaaaaa]]></info_title>"+
"<index_code><![CDATA[bbbbbbbbbb]]></index_code>"+
"<content><![CDATA[cccccccccc]]></content>"+
"<adjuncts>"+
"</adjuncts>"+
"</info>"+
"<info>"+
"<info_title><![CDATA[11111111111111]]></info_title>"+
"<index_code><![CDATA[2222222222222]]></index_code>"+
"<content><![CDATA[33333333333333]]></content>"+
"<adjuncts>"+
"<adjunct>"+
"<file_name><![CDATA[44444444444]]></file_name>"+
"<adjunct_content><![CDATA[5555555555555]]></adjunct_content>"+
"</adjunct>"+
"</adjuncts>"+
"</info>"+
"<info>"+
"<info_title><![CDATA[xxxxxxxxxxxxxxxxx]]></info_title>"+
"<index_code><![CDATA[yyyyyyyyyyyyyyyy]]></index_code>"+
"<content><![CDATA[zzzzzzzzzzzzzzzzzz]]></content>"+
"<adjuncts>"+
"<adjunct>"+
"<file_name><![CDATA[vvvvvvvvvvvvv]]></file_name>"+
"<adjunct_content><![CDATA[rrrrrrrrrrrrrrrr]]></adjunct_content>"+
"</adjunct>"+
"<adjunct>"+
"<file_name><![CDATA[ggggggggggggggggggggggg]]></file_name>"+
"<adjunct_content><![CDATA[llllllllllllllllll]]></adjunct_content>"+
"</adjunct>"+
"</adjuncts>"+
"</info>"+
"</infos>";
xml.doInfoXML(infoXML);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值