程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 經由過程dom4j解析xml字符串(示例代碼)

經由過程dom4j解析xml字符串(示例代碼)

編輯:關於JAVA

經由過程dom4j解析xml字符串(示例代碼)。本站提示廣大學習愛好者:(經由過程dom4j解析xml字符串(示例代碼))文章只能為提供參考,不一定能成為您想要的結果。以下是經由過程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”會以為是通俗屬性!
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved