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

用SAXReader解析xml文档

     使用SAXReader需要导入dom4j-full.jar包。
     dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的。dom4j是一个非常非常优秀的Java XML API,具有性能优异、功能强大和极端易用使用的特点,同时它也是一个开放源代码的软件,可以在SourceForge上找到它。
     使用举例:
1.    s.xml内容
<?xml version="1.0" encoding="GB2312"?>
<data>
<row queryDTO.enterpriseId="gfd" queryDTO.loginName="gdfg" queryDTO.state="0"/>
</data>

2.解析
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.tree.AbstractAttribute;
public class ReadXMLTest {

public static void main(String[] args){
File xmlFile = new File("C:/s.xml");
FileInputStream fis = null;
try {
fis = new FileInputStream(xmlFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.err.println("File is not exsit!");
}

SAXReader saxReader = new SAXReader();
List rowList = null;
try {
//生成文档对应实体
Document doc = saxReader.read(fis);
//获取指定路径下的元素列表,这里指获取所有的data下的row元素
rowList = doc.selectNodes("//data/row");
} catch (DocumentException e) {
e.printStackTrace();
}


for(Iterator iter = rowList.iterator();iter.hasNext();){
//获得具体的row元素
Element element = (Element)iter.next();
//获得row元素的所有属性列表
List elementList = element.attributes();
for(Iterator iter1 = elementList.iterator();iter1.hasNext();){
//将每个属性转化为一个抽象属性,然后获取其名字和值
AbstractAttribute aa = (AbstractAttribute)iter1.next();
System.out.println("Name:"+aa.getName


相关文档:

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

CMarkup与tinyXml直接解析XML字符串

今天才知道CMarkup可以直接解析字符串形式的XML。以前都是先存入一个文件,然后从文件中load。多做了I/O操作,效率不高。
CMarkup xml;
CString str;
xml.SetDoc(str);
tinyXml也可以直接解析XML字符串,方式如下:
// directly parsing string with tinyxml   
const char* ......

关于RDF与XML的区别

      刚开始学习语义网的知识,根据语义网层次推进,看完XML紧接着RDF,忽然有个疑问:为什么我们一定要用RDF而非仅仅使用已经很成熟的XML呢?貌似XML比RDF少了一个推理的系统,可以就推理而言,RDF也绝不是最好的,显然OWL的推理要比RDF强很多啊,那么为什么还需要这个层次呢?
  &n ......

JDOM生成XML文档

首先下载JDOM.JAR加入的classpath中
package com.test.search;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.output.XMLOutputter;
public class ToXML {
public ......

XML读取

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class XMLReader {
public static List ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号