`
fengbenming
  • 浏览: 4469 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

xml文件解析 document对象

阅读更多
一.Document对象相关                                                                      
                                                                                    
1.读取XML文件,获得document对象.                                                            
            SAXReader reader = new SAXReader();                                     
             Document  document = reader.read(new File("input.xml"));               
                                                                                    
                                                                                    
2.解析XML形式的文本,得到document对象.                                                         
             String text = "<members></members>";                                   
             Document document = DocumentHelper.parseText(text);                    
                                                                                    
3.主动创建document对象.                                                                  
             Document document = DocumentHelper.createDocument();                   
             Element root = document.addElement("members");// 创建根节点                 
                                                                                    
二. 节点相关                                                                            
                                                                                    
1. 获取文档的根节点.                                                                       
Element rootElm = document.getRootElement();                                       
                                                                                    
2.取得某节点的单个子节点.                                                                     
Element memberElm=root.element("member");// "member" 是节点名                          
                                                                                    
3.取得节点的文字                                                                          
String text=memberElm.getText();                                                   
也可以用:                                                                              
String text=root.elementText("name");                                              
这个是取得根节点下的name字节点的文字.                                                              
                                                                                    
4.取得某节点下名为"member"的所有字节点并进行遍历.                                                     
List nodes = rootElm.elements("member");                                           
                                                                                    
for (Iterator it = nodes.iterator(); it.hasNext();) {                              
    Element elm = (Element) it.next();                                              
    // do something                                                                 
}                                                                                  
                                                                                    
5.对某节点下的所有子节点进行遍历.                                                                 
             for(Iterator it=root.elementIterator();it.hasNext();){                 
                 Element element = (Element) it.next();                             
                 // do something                                                    
             }                                                                      
                                                                                    
6.在某节点下添加子节点.                                                                      
Element ageElm = newMemberElm.addElement("age");                                   
                                                                                    
7.设置节点文字.                                                                          
ageElm.setText("29");                                                              
                                                                                    
8.删除某节点.                                                                           
parentElm.remove(childElm);// childElm是待删除的节点,parentElm是其父节点                       
                                                                                    
三. 属性相关.                                                                           
1.取得某节点下的某属性                                                                       
             Element root=document.getRootElement();                                
             Attribute attribute=root.attribute("size");// 属性名 name                 
                                                                                    
2.取得属性的文字                                                                          
             String text=attribute.getText();                                       
也可以用:                                                                              
String text2=root.element("name").attributeValue("firstname");                     
这个是取得根节点下name字节点的属性firstname的值.                                                    
                                                                                    
3.遍历某节点的所有属性                                                                       
             Element root=document.getRootElement();                                
             for(Iterator it=root.attributeIterator();it.hasNext();){               
                 Attribute attribute = (Attribute) it.next();                       
                 String text=attribute.getText();                                   
                 System.out.println(text);                                          
             }                                                                      
                                                                                    
4.设置某节点的属性和文字.                                                                     
newMemberElm.addAttribute("name", "sitinspring");                                  
                                                                                    
5.设置属性的文字                                                                          
             Attribute attribute=root.attribute("name");                            
             attribute.setText("sitinspring");                                      
                                                                                    
6.删除某属性                                                                            
             Attribute attribute=root.attribute("size");// 属性名 name                 
             root.remove(attribute);                                                
                                                                                    
四. 将文档写入XML文件.                                                                     
1.文档中全为英文,不设置编码,直接写入的形式.                                                           
XMLWriter writer = new XMLWriter(new FileWriter("output.xml"));                    
writer.write(document);                                                            
writer.close();                                                                    
                                                                                    
2.文档中含有中文,设置编码格式写入的形式.                                                             
             OutputFormat format = OutputFormat.createPrettyPrint();                
             format.setEncoding("GBK");    // 指定XML编码                               
             XMLWriter writer = new XMLWriter(new FileWriter("output.xml"),format); 
                                                                                    
             writer.write(document);                                                
             writer.close();                                                        
                                                                                    
五. 字符串与XML的转换                                                                      
1.将字符串转化为XML                                                                       
String text = "<members> <member>sitinspring</member> </members>";                 
Document document = DocumentHelper.parseText(text);                                
                                                                                    
2.将文档或节点的XML转化为字符串.                                                                
             SAXReader reader = new SAXReader();                                    
             Document  document = reader.read(new File("input.xml"));               
             Element root=document.getRootElement();                                
             String docXmlText=document.asXML();                                    
             String rootXmlText=root.asXML();                                       
             Element memberElm=root.element("member");                              
             String memberXmlText=memberElm.asXML();                                
                                                                                    
六. 使用XPath快速找到节点.                                                                  
读取的XML文档示例                                                                         
<?xml version="1.0" encoding="UTF-8"?>                                             
<projectDescription>                                                               
   <name>MemberManagement</name>                                                    
   <comment></comment>                                                              
   <projects>                                                                       
     <project>PRJ1</project>                                                        
     <project>PRJ2</project>                                                        
     <project>PRJ3</project>                                                        
     <project>PRJ4</project>                                                        
   </projects>                                                                      
   <buildSpec>                                                                      
     <buildCommand>                                                                 
       <name>org.eclipse.jdt.core.javabuilder</name>                                
       <arguments>                                                                  
       </arguments>                                                                 
     </buildCommand>                                                                
   </buildSpec>                                                                     
   <natures>                                                                        
     <nature>org.eclipse.jdt.core.javanature</nature>                               
   </natures>                                                                       
</projectDescription>                                                              
                                                                                    
使用XPath快速找到节点project.                                                              
  public static void main(String[] args){                                           
     SAXReader reader = new SAXReader();                                            
                                                                                    
     try{                                                                           
       Document  doc = reader.read(new File("sample.xml"));                         
                                                                                    
       List projects=doc.selectNodes("/projectDescription/projects/project");       
                                                                                    
       Iterator it=projects.iterator();                                             
                                                                                    
       while(it.hasNext()){                                                         
         Element elm=(Element)it.next();                                            
         System.out.println(elm.getText());                                         
       }                                                                            
                                                                                    
     }                                                                              
     catch(Exception ex){                                                           
        ex.printStackTrace();                                                       
     }                                                                              
   }                                                                                
分享到:
评论

相关推荐

    如何用Document解析XML文件

    在Java中,使用`Document`对象解析XML文件是一种常见且有效的方式。本文将详细介绍如何利用Java标准库中的`Document`及相关类来解析和修改XML文件。 #### XML解析基础:`Document`及关联类 ##### ...

    XML文档解析相关工具

    2. **SAX解析**:除了DOM,dom4j还支持SAX(Simple API for XML)解析器,适用于处理大型XML文件,因为它不需要一次性加载整个文档到内存。 3. **XPath支持**:dom4j内置了XPath引擎,可以使用XPath表达式方便地定位...

    读取XML文件的四种方法

    使用DOM方法读取XML文件,需要创建DocumentBuilderFactory对象,获取DocumentBuilder对象,然后解析XML文件得到Document对象。最后,使用Document对象的getElementsByTagName方法获取指定标签的节点列表,并遍历节点...

    易语言 xml解析 易语言 xml解析 教程 带源码

    例如,通过`创建XML文档对象`、`加载XML文件`、`查找指定元素`、`获取属性值`等命令,可以实现对XML数据的读取。 4. **SAX解析**:与DOM不同,SAX解析器不将整个XML文档加载到内存,而是通过事件回调函数处理每个...

    tinyxml2解析XML文件读取数据

    - tinyxml2提供了DOM(Document Object Model)模型来处理XML文件,允许开发者以树形结构访问和修改XML文档。 - 库的主要类包括`XMLDocument`(XML文档对象)、`XMLElement`(XML元素)、`XMLText`(XML文本)、`...

    C++ XML文件解析库 tinyxml2

    1. **XML解析**:TinyXML2通过`XMLDocument`类来表示整个XML文档,你可以使用`LoadFile()`或`Parse()`方法加载XML文件或内存中的XML字符串。解析完成后,文档的结构被映射为一系列的节点,包括元素(XMLElement)、...

    QT解析xml文档 读取写入

    使用`QDomDocument::parse()`函数或者`QFile`与`QTextStream`结合,读取XML文件内容并解析成QDomDocument对象。 ```cpp QFile file("path_to_xml_file.xml"); file.open(QIODevice::ReadOnly | QIODevice::Text); ...

    用C++语言实现XML文件解析的一种方案

    ### 用C++语言实现XML文件解析的一种方案 #### 概述 随着互联网技术的不断发展,XML(可扩展标记语言)作为一种重要的数据交换格式,在各领域中的应用日益广泛。XML以其良好的可读性和开放性成为了数据共享的标准...

    android下解析xml文件的demo

    - 使用`parse()`方法解析XML文件,得到`Document`对象。 - 使用`getElementsByTagName()`等方法获取XML节点,并通过`getTextContent()`或`getAttributes()`等方法获取节点内容和属性。 2. SAX解析: SAX解析器是...

    Sax解析XML文件解析

    SAX(Simple API for XML)是XML解析的一种方法,它以事件驱动的方式处理XML文档,非常适合处理大体积的XML文件,因为它不需要一次性加载整个文件到内存,而是逐行读取,因此节省了资源。 **SAX解析XML的基本原理:...

    Tinyxml文件解析经典例子

    Tinyxml 文件解析经典例子 TinyXML 是一个开源的解析 XML 的解析库,能够用于 C++,能够在 Windows 或 Linux 中编译。这个解析库的模型通过解析 XML 文件,然后在内存中生成 DOM 模型,从而让我们很方便的遍历这棵...

    xml文件的解析

    2. 使用`DocumentBuilder`解析XML文件,得到`Document`对象。 3. 使用`Document`对象的方法查找、添加、删除或修改元素。 4. 修改后的文档需要重新写回到XML文件。 DOM4J是另一个流行的Java XML处理库,它提供了更...

    js解析XML文件

    JS 解析 XML 文件 作为一名 IT 行业大师,我将从给定的...使用 JavaScript 解析 XML 文件需要了解 XML 文档的结构和各个节点的类型,选择合适的解析方法,考虑浏览器兼容性问题,并掌握相关的 JavaScript 语法和方法。

    java 解析xml 多级

    使用解析器的`parse()`方法加载XML文件到`Document`对象。 ```java File xmlFile = new File("test.xml"); Document doc = builder.parse(xmlFile); doc.getDocumentElement().normalize(); ``` 4. 遍历XML结构: ...

    xml.rar_XML 文档 解析_XML 解析_XML文档

    这个"xml.rar"压缩包包含了一系列与XML文档解析相关的源代码文件,对于初学者来说,是理解XML解析机制的一个很好的实例。 1. **XML文档结构**: XML文档由一系列元素组成,每个元素通过开始标签 `&lt;tag&gt;` 和结束...

    java解析XML文件

    1. **读取XML文件**:使用`org.dom4j.io.SAXReader`类创建一个SAXReader实例,然后使用`read()`方法读取XML文件到Document对象。 ```java import org.dom4j.Document; import org.dom4j.DocumentException; import ...

    使用JDOM解析XML文件

    JDOM通过Document对象表示整个XML文档,Element代表XML元素,Attribute表示元素属性,Text表示元素内的文本内容。这些类构成了JDOM的核心,它们帮助开发者构建和操作XML文档。 在解析XML文件时,JDOM提供了...

    VC xml文件解析类

    1. **构造函数**:初始化类对象,可能接受XML文件路径作为参数,用于后续的读取操作。 2. **打开文件**:使用C++标准库中的`fstream`或第三方库(如TinyXML、pugixml等)来打开并读取XML文件。 3. **解析XML结构**...

    android xml pull解析示例

    - 设置输入源:将XML文档的输入源(可能是文件、网络流、字符串等)设置到解析器。 - 迭代事件:调用`next()`方法,遍历XML文档中的每一个事件。事件包括START_DOCUMENT、START_TAG、END_TAG、TEXT等。 - 处理...

Global site tag (gtag.js) - Google Analytics