dom4j解析RDF/XML文件
xml
<?xml version="1.0" encoding="UTF-8" ?>
<rdf:RDF xmlns:sgcc="http://www.sgcc.com.cn/cim#" xmlns:cim="http://iec.ch/TC57/IEC-CIM6197014v15-6196810v31#" xmlns:sawsdl="http://www.w3.org/ns/sawsdl" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<cim:PSRType rdf:ID="PD_10000100">
<cim:IdentifiedObject.mRID>10000100</cim:IdentifiedObject.mRID>
<cim:IdentifiedObject.name>北京</cim:IdentifiedObject.name>
</cim:PSRType>
<cim:PSRType rdf:ID="PD_20100000">
<cim:IdentifiedObject.mRID>20100000</cim:IdentifiedObject.mRID>
<cim:IdentifiedObject.name>海淀</cim:IdentifiedObject.name>
</cim:PSRType>
<cim:PSRType rdf:ID="PD_20200000">
<cim:IdentifiedObject.name>上地</cim:IdentifiedObject.name>
</cim:PSRType>
<cim:PSRType rdf:ID="PD_20300000">
<cim:IdentifiedObject.name>http://www.test.com</cim:IdentifiedObject.name>
</cim:PSRType>
<cim:Disconnector rdf:ID="PD_30600000_416822">
<cim:Switch.open>true</cim:Switch.open>
<cim:Switch.normalOpen>false</cim:Switch.normalOpen>
<cim:IdentifiedObject.mRID>30600000_416822</cim:IdentifiedObject.mRID>
<cim:IdentifiedObject.name>3073</cim:IdentifiedObject.name>
<cim:SystemResource.Test1 rdf:resource="#PD_10000100" />
<cim:SystemResource.Test2 rdf:resource="#PD_20100000" />
<cim:SystemResource.Test3 rdf:resource="#PD_20200000" />
<cim:SystemResource.Test4 rdf:resource="#PD_20300000" />
</cim:Disconnector>
</rdf:RDF>
dom4j解析
package com.test;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.util.List;
/**
* @author xiaodong
*/
public class test2 {
public static void main(String[] args) throws DocumentException {
SAXReader reader = new SAXReader();
File file = new File("src/test/java/com/test/test001.xml");
Document document = reader.read(file);
Element root = document.getRootElement();
System.out.println("总节点名:"+root.getName());
List<Element> childElements = root.elements();
for (Element child : childElements) {
//未知属性名情况下
List<Attribute> attributeList = child.attributes();
for (Attribute attr : attributeList) {
System.out.println("节点名:"+child.getName());
System.out.println("属性名:"+attr.getName()+"--->属性值: " + attr.getValue());
}
//未知子元素名情况下
List<Element> elementList = child.elements();
for (Element ele : elementList) {
if (ele.getTextTrim()!=null && ele.getTextTrim().length()!=0) {
System.out.println("属性名:" + ele.getName() + "--->属性值:" + ele.getTextTrim());
}
// 当前节点的所有属性
final List<Attribute> list = ele.attributes();
// 遍历当前节点的所有属性
for (final Attribute attr : list) {
// 属性名称
/*final String name = attr.getName();*/
// 属性的值
final String value = attr.getValue();
if (value.charAt(0)=='#'){
//去掉关联符 #
System.out.println("属性名:"+ele.getName() + "--->属性关联:" + value.substring(1));
}else {
System.out.println("属性名:"+ele.getName() + "--->属性关联:" + value);
}
}
}
System.out.println("-----------------------------------------");
//已知属性名情况下
/*System.out.println("id: " + child.attributeValue("id"));*/
//已知子元素名的情况下
/*System.out.println("name" + child.elementText("name"));
System.out.println("address" + child.elementText("address"));*/
}
}
}
获取xml文件路径
test001.xml→右键→点击copy path