SaxReader

阅读更多

 

package demo;

import java.util.List;

import org.dom4j.Document;

import org.dom4j.Element;

public class XmlDocument {

@SuppressWarnings("unchecked")

public static void main(String[] args) {

//工具类从某文件中记载Document

Document doc = XmlUtil.parseToDocument("f:/xml.txt");

if(doc == null){

return;

}

 

System.out.println("文档完整内容:"+doc.asXML());

System.out.println("文档名称:"+doc.getName());

System.out.println("文档类型:"+doc.getNodeTypeName());

System.out.println("文档路径:"+doc.getPath());

System.out.println("文件编码:"+doc.getXMLEncoding());

System.out.println("+++++++++++++++++");

Element rootE = doc.getRootElement();

System.out.println("根节点属性个数:"+rootE.attributeCount());

System.out.println("根节点id属性值:"+rootE.attributeValue("id"));

System.out.println("根节点name属性值:"+rootE.attributeValue("name"));

System.out.println("根节点路径:"+rootE.getPath());

System.out.println("+++++++++++++++++");

Element countE = rootE.element("count");

System.out.println(countE.attributeValue("desc")+":"+countE.getText());

System.out.println("+++++++++++++++++");

//result节点集合

List resultList = rootE.elements("result");

System.out.println("result节点个数:"+resultList.size());

for(Element e : resultList){

System.out.print(e.attributeValue("order")+"\t");

System.out.print(e.element("song").getTextTrim()+"\t");

System.out.print(e.element("songer").getTextTrim()+"\t");

System.out.print(e.element("songid").getTextTrim()+"\t");

System.out.println(e.element("songurl").getTextTrim()+"\t");

}

}

}

 

f:/xml.txt文件内容如下:

2

在我的歌声里

曲婉婷

6952850

music.url.*

我的歌声里

李代沫

7640224

music.url.*


  • dom4j-1.6.1.jar (306.5 KB)
  • 下载次数: 3

你可能感兴趣的:(saxreader,dom4j,doc,xml)