用dom来解析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>
用DOM来解析类:
package com.pk.xml;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class DOMxml {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
//获取dom工厂
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//获取dom解析器
DocumentBuilder builder = factory.newDocumentBuilder();
//解析的文件
File file = new File("D:\\项目\\me\\mobilelist.xml");
Document document = builder.parse(file);
//获取根节点
Element root = document.getDocumentElement();
//获取子节点列表
NodeList books = root.getChildNodes();
for(int i= 0 ; i<books.getLength();i++){
//获取每一个子节点
Node book = books.item(i);
if(book.getNodeType()==Node.ELEMENT_NODE){
//获取属性的值
String type = book.getAttributes().getNamedItem("type").getNodeValue();
System.out.print(type+"\t");
//循环子节点
for(Node node=book.getFirstChild();node!=null;node=node.getNextSibling()){
if(node.getNodeType()==Node.ELEMENT_NODE){
if(node.getNodeName().equals("wap2")){
String wap2 = node.getFirstChild
相关文档:
1、arraycollection转化为xml,代码如下:
//动态生成树形结构
public static function flatArrayToXML(arr:Object,rootname:String=null,nodename:String=null, outputString:Boolean=false):Object{
if (arr is Array){
......
xml -声明-引发的异常
XML 声明 [XML 标准]
XML 声明通常在 XML 文档的第一行出现。XML 声明不是必选项,但是如果使用 XML 声明,必须在文档的第一行,前面不得包含任何其他内容或空白。
文档映射中的 XML 声明包含下列内容:
版本号 <?xml version="1.0"?>。
这是必选项。尽管以后的 XML 版本可能会更改该 ......
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 ......
xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<mobile-list>
<mobile type="Nokia2652">
<wap2>false</wap2>
<width>115</width>
</mobile>
<mobile type="Nokia2650">
......