dom4j 解析xml string 字符串

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); 

你可能感兴趣的:(String)