1.引用dom4j的maven包
//maven包
<!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jaxen/jaxen -->
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.6</version>
</dependency>
2.创建Document对象
SAXReader saxReader = new SAXReader(new DocumentFactory());
Document document = null;
try
{
//inputStream=getXmlFilePath().getInputStream();
document = saxReader.read(new ByteArrayInputStream());//读取某一个流对象,如文件流、字节流
}catch(Exception ex)
{
ex.getMessage();
}
3.获取根节点及其子节点
//获取文档根节点
Element root = document.getRootElement();
//获取根节点下面的所有子节点(不包过子节点的子节点)
List<Element> list = root.elements() ;
//获得指定节点下面的子节点
Element contactElem = root.element("PROJECTINFO");//首先要知道自己要操作的节点。
String iteminfo=contactElem.attributeValue("TABLE");//PROJECTINFO节点的属性,下同
String xh=contactElem.attributeValue("PHASE");
String id=contactElem.attributeValue("ID_COLUMN_NAME");
List<Element> contactList = contactElem.elements();//获取PROJECTINFO节点下的全部子节点
//对节点进行操作。。。。