易截截图软件、单文件、免安装、纯绿色、仅160KB

xml解析与创建


下午看了一下dom和dom4j,感觉这两者在解析方面差不多,但是dom4j要比dom简单一些,在创建一个xml文件,dom4j要比dom简单太多了,毕竟人家都帮我们封装好了。
dom是不需要加包的,而dom4j要加一个dom4j-1.6.1.jar这个包。具体的下载路径是:http://www.dom4j.org/
下面我说一下这两者如何解析和创建,以及他们的区别。xml个人觉得是蛮重要的,因为它是可扩展的,怎么说呢,xml也许会代替html。
dom的解析的代码如下:
private static void read() {
File file = new File("src/xml/student.xml");
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(file);
NodeList node = doc.getChildNodes();
for (int i = 0; i < node.getLength(); i++) {
Element e = (Element) node.item(i);
// System.out.println(e.getNodeName());
boolean flag = e.hasChildNodes();
if (flag) {
NodeList list = e.getChildNodes();
for (int j = 0; j < list.getLength(); j++) {
e = (Element) list.item(i);
System.out.println(e.getNodeName());
}
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
这个只是解析到第二层,接下去的解析与第二层的解析是一样的。。我简单的说一下步骤吧:
首先创建一个document工厂,通过工厂来得到DocumentBuilder ,再由builder来得到document。builder.parse(file)可以得到一个file文件,同时丢出ParserConfigurationException这个异常。
xml的解析、创建都是基于这个document的,在创建一个xml也是同样的方法,要先创建一个document。
再接下来通过document得到一系列的子结点,NodeList与List还是有所区别的,在list里面有一个iterator,但是遍历NodeList只能用for循环。在list里面得到元素,是用list.get(i),而NodeList是list.item(i)拿到子元素。接下来就是判断子元素里面是不是还有子元素。
dom4j的解析的代码如下:
private static void read() {
File file=new File("src/xml/writer.xml");
SAXReader reader=new


相关文档:

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/"
     ......

LINQ TO XML Common Class

个人收集、整理了一些LINQ TO XML的基本方法,希望各位大虾多多指导:
 /// <summary>
///Xml节点属性
/// </summary>
public class XmlAttribute
{
 public XmlAttribute()
 {
  
 }
    public XmlAttribute(string _key,object _value)
 &nbs ......

使用SimpleXML函数来加载和解析XML文档

 大量SmipleXML函数可用来加载和解析大量XML文档。
1.simpleXML_load_file()函数来加载指定的XML文件到对象。如果加载文件时遇到问题,则返回FLASE。例:
book.xml文件:
<?xml version="1.0" standalone="yes"?>
<library>
<book>
<title>Pride and Prejudice</title>
< ......

用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</wa ......

php之XML转数组函数

<?
/**
* xml2array() will convert the given XML text to an array in the XML structure.
* Link: http://www.bin-co.com/php/scripts/xml2array/
* Arguments : $contents - The XML text
* $get_attributes - 1 or 0. If this is 1 the function will get the attributes as well as the ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号