java 使用 xml 尋找遍曆

來源:互聯網
上載者:User

<?xml version="1.0" encoding="gb2312"?>

<root>
 <LINE>
  <種類>動物</種類>
 </LINE>
 <LINE>
  <編號>001</編號>
  <名稱>牛</名稱>
 </LINE>
 <LINE>
  <種類>植物</種類>
 </LINE>
 <LINE>
  <編號>001</編號>
  <名稱>松樹</名稱>
 </LINE>
 <LINE>
  <編號>002</編號>
  <名稱>玫瑰</名稱>
 </LINE>
</root>
 

然後用下面程式,查詢編號為001的LINE元素,並顯示它們的名稱。

test.java

package mypack;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.jdom.*;
import org.jdom.input.*;
import org.jdom.xpath.XPath;

public class test {
 public static void main(String[] args) {
  File f=new File("test.xml");
  SAXBuilder saxb=new SAXBuilder();
  try {
   Document xmldoc=saxb.build(f);
   XPath xpath=XPath.newInstance("//LINE[編號='001']");//建立一個查詢編號為001的LINE元素的XPATH。
   List nodes=xpath.selectNodes(xmldoc);//進行查詢
   System.out.println("返回記錄數:"+nodes.size());
   for(int i=0;i<nodes.size();i++) {
    System.out.print(i+" / ");
    System.out.println(((Element)nodes.get(i)).getChildText("名稱"));//返回該LINE的名稱的文本值。
   }
  } catch (JDOMException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }


//列印xml文檔 private void parseElement(Element root) {  //System.out.print(root.getNamespaceURI());    System.out.print("<");  System.out.print(root.getNodeName());  //System.out.print(root.getPrefix());  //System.out.print(":");  //System.out.print(root.getLocalName());    NamedNodeMap nnm = root.getAttributes();  for(int i = 0; i < nnm.getLength(); i++)  {   Attr attr = (Attr)nnm.item(i);   System.out.print(" ");   System.out.print(attr.getName());   System.out.print("=\"");   System.out.print(attr.getValue());   System.out.print("\"");  }    System.out.print(">");    NodeList list = root.getChildNodes();  for(int i = 0; i < list.getLength(); i++)  {   Node node = list.item(i);   if(node instanceof Element)   {    Element e = (Element)node;    parseElement(e);   }   else if(node instanceof Text)   {    Text t = (Text)node;    System.out.print(t.getNodeValue());   }  }    System.out.print("</");  System.out.print(root.getNodeName());  System.out.print(">"); }  private void parseRootName() {  Element root = doc.getDocumentElement();  System.out.println(root.getNodeName()); } //工廠 private void getDocument() {  try  {   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();      factory.setNamespaceAware(true);      DocumentBuilder db = factory.newDocumentBuilder();   doc = db.parse(new File(fileName));  }  catch(Exception ex)  {   ex.printStackTrace();   System.exit(1);  } }

 }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.