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
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文件内容如下: