欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

通过dom4j解析xml字符串(示例代码)

程序员文章站 2023-12-22 21:36:10
复制代码 代码如下:import java.util.iterator;import java.util.list;import org.dom4j.attribute;i...
复制代码 代码如下:

import java.util.iterator;
import java.util.list;
import org.dom4j.attribute;
import org.dom4j.document;
import org.dom4j.documenthelper;
import org.dom4j.element;
public class test {
 @suppresswarnings("unchecked")
 public static void main(string[] args) throws exception {
  string xml = "<request> <param name='service'>single_trade_query </param><param name='_input_charset'>utf-8 </param><param name='partner'>2088001513232645 </param><param name='out_trade_no'>20090422577264 </param></request>";
  document document = documenthelper.parsetext(xml);
  element root = document.getrootelement();
  list<element> elements = root.elements();
  for (iterator<element> it = elements.iterator(); it.hasnext();) {
   element element = it.next();
   list<attribute> attributes = element.attributes();
   for (int i = 0; i < attributes.size(); i++) {
    attribute attribute = attributes.get(i);
    if ("service".equals(attribute.gettext())) {
     system.out.println(element.getname() + "  :  "
       + element.gettext());
    }
   }
  }
 }
}

或者:
复制代码 代码如下:

import java.io.stringreader;
import java.util.iterator;
import java.util.list;
import org.dom4j.document;
import org.dom4j.element;
import org.dom4j.io.saxreader;
import org.xml.sax.inputsource;
public class test {

 @suppresswarnings("unchecked")
 public static void main(string[] args) throws exception {
  string xml = "<servlet><description></description><display-name>testservlet</display-name><servlet-name>testservlet</servlet-name><servlet-class>test.testservlet</servlet-class></servlet>";
  inputsource in = new inputsource(new stringreader(xml));
  in.setencoding("utf-8");
  saxreader reader = new saxreader();
  document document = reader.read(in);
  element root = document.getrootelement();
  list<element> elements = root.elements();
  for(iterator<element> it = elements.iterator();it.hasnext();){
   element element = it.next();
   system.out.println(element.getname()+" : "+element.gettexttrim());
  }

 }
}

或者增加id属性,直接通过id获取某个属性值:
复制代码 代码如下:

import java.io.stringreader;
import org.dom4j.document;
import org.dom4j.documentexception;
import org.dom4j.element;
import org.dom4j.io.saxreader;
import org.xml.sax.inputsource;
public class test {
    public static void main(string args[]) {
     string xml = "<request> <param name='service' id='tt'>single_trade_query </param><param name='_input_charset'>utf-8 </param><param name='partner'>2088001513232645 </param><param name='out_trade_no'>20090422577264 </param></request>";
     inputsource source = new inputsource(new stringreader(xml));
        saxreader reader = new saxreader();
        document document = null;
  try {
   document = reader.read(source);
  } catch (documentexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  }
  element root = document.getrootelement();
        system.out.println(document.elementbyid("tt").gettexttrim());
        system.out.println(root.elementbyid("tt").gettexttrim());
    }
}

注意:通过id获取,元素id属性名必须为“大写id”,小写的“id”会认为是普通属性!

上一篇:

下一篇: