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.doInfoXML(infoXML);
}
}