服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - JAVA教程 - 通过dom4j解析xml字符串(示例代码)

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

2019-10-26 16:47java教程网 JAVA教程

本篇文章主要是对通过dom4j解析xml字符串的示例代码进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助

代码如下:


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”会认为是普通属性!

延伸 · 阅读

精彩推荐