解析xml文件的几种常见的方法
xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<mobile-list>
<mobile type="Nokia2652">
<wap2>false</wap2>
<width>115</width>
</mobile>
<mobile type="Nokia2650">
<wap2>false</wap2>
<width>115</width>
</mobile>
<mobile type="Nokia6108">
<wap2>false</wap2>
<width>115</width>
</mobile>
</mobile-list>
-----------------------------------------------------
-----------------------------------------------------
使用jdom读xml文件:
package com.pk.xml;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
public class JDOMxml {
public static void main(String[] args) throws JDOMException, IOException {
try{
//获取解析器
SAXBuilder builder = new SAXBuilder();
//解析文件
File file = new File("D:\\项目\\me\\mobilelist.xml");
Document document = builder.build(file);
//取根节点
Element root = document.getRootElement();
//取的节点列表
List books = root.getChildren();
for(int i = 0 ; i<books.size();i++){
//取得某一个节点
Element book = (Element) books.get(i);
//获取属性值
String type = book.getAttributeValue("type");
System.out.print(type+"\t");
String text = book.getChild("wap2").getText();
String texts = book.getChild("width").getText();
System.out.print(text+"\t");
System.out.println(texts+"\t");
}
}catch (Exception e) {
e.printStackTrace();
相关文档:
xml -声明-引发的异常
XML 声明 [XML 标准]
XML 声明通常在 XML 文档的第一行出现。XML 声明不是必选项,但是如果使用 XML 声明,必须在文档的第一行,前面不得包含任何其他内容或空白。
文档映射中的 XML 声明包含下列内容:
版本号 <?xml version="1.0"?>。
这是必选项。尽管以后的 XML 版本可能会更改该 ......
wsdl.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<definitions name="MobilePhoneService"
targetNamespace="www.mobilephoneservice.com/MobilePhoneService-interface"
xmlns="http://schemas.xmlsoap.org/wsdl/"
  ......
book_schema.xml文件
<?xml version="1.0" encoding="gb2312"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="丛书">
<xs:complexType>
<xs:sequence>
<xs:element name="书">
&n ......
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
<mx:XML id="xmlSource">
<node label="grandFather" state="unchecked">
<node label="Father" state="un ......
声明
/// <summary>
/// XML文档
/// </summary>
XmlDocument xmldoc;
&n ......