激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java遍歷讀取xml文件內容

java遍歷讀取xml文件內容

2019-06-16 21:24腳本之家 Java教程

這篇文章主要為大家介紹了java遍歷讀取xml文件內容,感興趣的小伙伴們可以參考一下

本文實例講解了java遍歷讀取xml文件內容的詳細代碼,分享給大家供大家參考,具體內容如下

package test;
 
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
 
import javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
 
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMAttribute;
import org.apache.axiom.om.OMComment;
import org.apache.axiom.om.OMContainer;
import org.apache.axiom.om.OMDataSource;
import org.apache.axiom.om.OMDocType;
import org.apache.axiom.om.OMDocument;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMException;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMProcessingInstruction;
import org.apache.axiom.om.OMSourcedElement;
import org.apache.axiom.om.OMText;
import org.apache.axiom.om.OMXMLParserWrapper;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.xml.sax.helpers.XMLReaderFactory;
 
public class Axiomtest {
 public static void main(String[] args) throws FileNotFoundException, Throwable {
//  read xml
    FileInputStream xmlFile = new FileInputStream("line-item2.xml");
    XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(xmlFile);
 
    // 還需要StAXOMBuilder對象
    StAXOMBuilder builder = new StAXOMBuilder(parser);
    
    OMElement doc = builder.getDocumentElement();   //  讀到<fool></fool>    
    
    OMElement cre = doc.getFirstChildWithName(new QName("student")); //讀到<student>
    
    OMElement cre1 = cre.getFirstChildWithName(new QName("id")); //  讀到<id></id>
    System.out.println(cre1.getLocalName()+":"+cre1.getText());
    cre1 = cre.getFirstChildWithName(new QName("name"));    //  讀到<name></name>
    System.out.println(cre1.getLocalName()+":"+cre1.getText()); 
   
    cre1 = cre.getFirstChildWithName(new QName("age"));   //  讀到<age></age>
    System.out.println(cre1.getLocalName()+":"+cre1.getText());  
    
    cre1 = cre.getFirstChildWithName(new QName("sex"));   //  讀到<sex></sex>
    System.out.println(cre1.getLocalName()+":"+cre1.getText());
    
    cre1 = cre.getFirstChildWithName(new QName("message"));   //  讀到<sex></sex>
    System.out.println(cre1.getLocalName()+":"+cre1.getText());
    
    System.out.println("------------------------------1");
    Iterator<OMElement> iter = doc.getChildElements();
    while(iter.hasNext()){
      OMElement temp = iter.next();
      System.out.println("====================");
      System.out.println(temp.getLocalName());
//      System.out.println(temp.getText());
 
      if(temp.getLocalName().equals("student")){
        Iterator<OMElement> iter1 = temp.getChildElements();
        System.out.println("----------------");
        while(iter1.hasNext()){
          OMElement temp1 = iter1.next();          
          System.out.println(temp1.getLocalName()+":"+temp1.getText());
        }
      }
    }
    System.out.println("!!!!!!!!!!!!!");
    FileInputStream file = new FileInputStream("line-item2.xml");
    XMLStreamReader read = XMLInputFactory.newInstance().createXMLStreamReader(file);
    StAXOMBuilder sta = new StAXOMBuilder(read);
    OMElement all = sta.getDocumentElement();
    Iterator<OMElement> ite1 = all.getChildElements();
    while(ite1.hasNext()){
      OMElement temp = ite1.next();
      if(temp.getLocalName().equals("student")){
       Iterator<OMElement> ite2 = temp.getChildElements();
       while(ite2.hasNext()){
         OMElement temp1 = ite2.next();
         System.out.println(temp1.getLocalName()+":"+temp1.getText());
      }     
    }
   }    
//    write xml
    
    OMFactory factory = OMAbstractFactory.getOMFactory();
    
    //建立doc節(jié)點,doc節(jié)點會和下面的root節(jié)點合并
    OMDocument dod = factory.createOMDocument();
    
    //建立root節(jié)點
    OMElement root = factory.createOMElement("root","","");
    OMElement add = factory.createOMElement("dabi","","");
    //建立兩個普通節(jié)點
    OMElement stu = factory.createOMElement("student","","");
    stu.addChild(factory.createOMText("mac"));
 
    OMElement tea = factory.createOMElement("teacher","","");
    tea.addChild(factory.createOMText("silly"));
    
    //構建樹,將兩個普通節(jié)點連到root節(jié)點上
    root.addChild(stu);
    root.addChild(tea);
    //構建樹,將root節(jié)點連到doc節(jié)點上
    dod.addChild(root);
    
    // 構建writer做輸出器
    XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(
        new FileOutputStream("2.xml"));
    root.serialize(writer); // cache on
    writer.flush();
    
    FileInputStream xmlFile1 = new FileInputStream("2.xml");
    XMLStreamReader parser1 = XMLInputFactory.newInstance().createXMLStreamReader(xmlFile1);
    
    StAXOMBuilder builder1 = new StAXOMBuilder(parser1);
    OMElement doc1 = builder1.getDocumentElement();
    
    Iterator<OMElement> iter1 = doc1.getChildElements();
    while(iter1.hasNext()){
      OMElement temp = iter1.next();
      System.out.println("====================");
      System.out.println(temp.getLocalName()+":"+temp.getText());
    }
 
    
    System.out.println("!!!!!!!!");
 
    OMFactory omf = OMAbstractFactory.getOMFactory();
//    OMDocument od = omf.createOMDocument();
    OMElement root1 = omf.createOMElement("root","","");
    OMElement name = omf.createOMElement("name","","");
    OMElement sex = omf.createOMElement("sexy","","");
    sex.addChild(omf.createOMText("man"));
    name.addChild(omf.createOMText("dabi"));
    root1.addChild(sex);
    root1.addChild(name);
//    od.addChild(root1);
    
    XMLStreamWriter xmlw = XMLOutputFactory.newInstance().createXMLStreamWriter(new FileOutputStream("3.xml"));
    root1.serialize(xmlw);
    
    xmlw.flush();
 }
}
<?xml version="1.0" encoding="UTF-8"?>
<fool>
  <student>
    <name>mac</name>
    <id>12</id>
    <age>33</age>
    <sex>male</sex>
    <message>hello world</message>
  </student>
  <student>
    <name>silly</name>
    <id>5</id>
    <age>12</age>
    <sex>female</sex>
  </student>
  <teacher>
    <name>Mr. Jones</name>
    <id>2</id>
    <age>31</age>
    <sex>male</sex>
  </teacher>
  <student>
    <name>macy</name>
    <id>2</id>
    <age>40</age>
    <sex>female</sex>
  </student>
  <student>
    <name>tom</name>
    <id>32</id>
    <age>31</age>
    <sex>male</sex>
  </student>
  <message>hello world</message>
</fool>

再分享一例: 用JAVA讀取XML文件

解析XML的步驟如下:

  •   1.創(chuàng)建DocumentBuilder工廠
  •   2.創(chuàng)建DocumentBuilder對象
  •   3.DocumentBuilder對象的parse方法得到Document對象
  •   4.Document對象的getElementsByTagName得到NodeList集合
  •   5.通過getFirstChild和getNextSibling進行遍歷 

用到的包:

  • import javax.xml.parsers.*;
  • import org.w3c.dom.*;
  • import org.xml.sax.*;

用到的對象:

  • DocumentBuilderFactory:創(chuàng)建DocumentBuilder的抽象工廠
  • DocumentBuilder:可以從 XML 獲取一個 Document
  • Document:提供供對文檔數(shù)據(jù)的基本訪問

用到的方法:

  • DocumentBuilder.parse(String)':將給定 URI 的內容解析為一個 XML 文檔,并且返回一個新的 DOM Document對象
  • Document.getElementsByTagName(String)':返回具有給定標記名稱的所有 Element 的 NodeList
  • Element.getAttribute(String)':通過名稱獲得屬性值

下面來解析一個XML文件

import javax.xml.parsers.*; 
import org.w3c.dom.*; 
import org.xml.sax.*; 
 
public class Test 
{ 
  public static void main(String[] args) 
  { 
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
    try 
    { 
      DocumentBuilder db = dbf.newDocumentBuilder(); 
      Document doc = db.parse("pet2.xml"); 
 
      NodeList dogList = doc.getElementsByTagName("dog"); 
      System.out.println("共有" + dogList.getLength() + "個dog節(jié)點"); 
      for (int i = 0; i < dogList.getLength(); i++) 
      { 
        Node dog = dogList.item(i); 
        Element elem = (Element) dog; 
        System.out.println("id:" + elem.getAttribute("id")); 
        for (Node node = dog.getFirstChild(); node != null; node = node.getNextSibling()) 
        { 
          if (node.getNodeType() == Node.ELEMENT_NODE) 
          { 
            String name = node.getNodeName(); 
            String value = node.getFirstChild().getNodeValue(); 
            System.out.print(name + ":" + value + "\t"); 
          } 
        } 
        System.out.println(); 
      } 
    } 
    catch (Exception e) 
    { 
      e.printStackTrace(); 
    } 
  } 
} 

XML文件

<pets> 
  <dogs> 
    <dog id="1">      
      <name>YAYA</name> 
      <health>100</health> 
      <love>0</love> 
      <strain>酷酷的雪娜瑞</strain> 
    </dog> 
    <dog id="2">      
      <name>OUOU</name> 
      <health>90</health> 
      <love>15</love> 
      <strain>聰明的拉布拉多犬</strain> 
    </dog> 
  </dogs> 
  <penguins> 
    <penguin id="3">      
      <name>QQ</name> 
      <health>100</health> 
      <love>20</love> 
      <sex>Q仔</sex>       
    </penguin>     
  </penguins> 
</pets> 

以上就是本文的全部內容,希望對大家的學習有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久久一区二区三区精品 | 欧美日本另类 | 香蕉久久久 | 日韩免费黄色 | 亚洲影视在线观看 | 多男操一女视频 | 天天操天天干天天操 | 久久恋 | 久久蜜臀一区二区三区av | 欧美一区二区精品夜夜嗨 | 久久久久九九九女人毛片 | 国产毛片在线看 | 国产精品99久久久久久大便 | 九九色网站 | 一级毛片免费大片 | 欧美一级特黄aaaaaaa什 | 日本精品视频一区二区三区四区 | 久久国产精品二国产精品中国洋人 | 国产大片全部免费看 | 免费观看一区二区三区 | 日韩精品中文字幕在线观看 | 91精品国产乱码久久久久久久久 | 亚洲影视在线观看 | 5xx免费看| 一区视频 | 能看的毛片网站 | 欧美精品网址 | 羞羞视频2023 | 福利在线免费 | 久久嗨| 久久sp| 看国产毛片 | 九九热欧美 | 爱性久久久久久久 | 久久一区二区三区av | 亚洲精品一区二区三区大胸 | aa级黄色片| 精品999www| 久久96国产精品久久久 | 99久久久国产精品露出 | 久久噜噜噜 |