DOM4j解析xml

--------------------------------------------------
DOM4j解析xml
解析XML


--------------------------------------------------
TestXmlUtil.java
package xml;

import java.io.FileNotFoundException;

import org.dom4j.DocumentException;

public class TestXmlUtil {

public static void main(String[] args) {

XmlUtil x = new XmlUtil();

try {
x.build("jbpm4/jpdl/process.jpdl.xml");
} catch (FileNotFoundException e) {

e.printStackTrace();
} catch (DocumentException e) {

e.printStackTrace();
}

x.printAllNodes(x.listChildNodes("process/start/transition"));

System.out.println("END");

}
}

--------------------------------------------------
XmlNode.java
package xml;

import org.dom4j.Element;

public class XmlNode {

private String name;
private String fullName;
private String pathName;
private String split = "-->";
private String path = "/";
private Element node;

public String getSplit() {
return split;
}

public void setSplit(String split) {
this.split = split;
}

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getFullName() {
return fullName;
}

public void setFullName(String fullName) {

StringBuffer buffer = new StringBuffer();
this.recursionFullName(buffer, node);
this.fullName = buffer.toString().substring(0,
buffer.length() - this.split.length());
}

public String getPathName() {
return pathName;
}

public void setPathName(String pathName) {
StringBuffer buffer = new StringBuffer();
this.recursionPathName(buffer, node);
this.pathName = buffer.toString().substring(0,
buffer.length() - this.path.length());

}

public void recursionFullName(StringBuffer buffer, Element node) {

String nodeName = node.getName();

if (node.getParent() == null) {

String temp = nodeName + this.split + buffer.toString();

buffer.delete(0, buffer.length());
buffer.append(temp);

return;
} else {
String temp = nodeName + this.split + buffer.toString();

buffer.delete(0, buffer.length());
buffer.append(temp);
recursionFullName(buffer, node.getParent());

}

}

public void recursionPathName(StringBuffer buffer, Element node) {

String nodeName = node.getName();

if (node.getParent()==null) {

String temp = nodeName + this.path + buffer.toString();

buffer.delete(0, buffer.length());
buffer.append(temp);

return;
} else {
String temp = nodeName + this.path + buffer.toString();

buffer.delete(0, buffer.length());
buffer.append(temp);
recursionPathName(buffer, node.getParent());

}

}

public Element getNode() {
return node;
}

public void setNode(Element node) {
this.node = node;
}

}

--------------------------------------------------
XmlUtil.java
package xml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class XmlUtil {

private Document document = null;

private Element elementRoot = null;

public String getExpr() {

List<Element> list = this.getRoot().elements();

for (Element e : list) {

if (e.getName().equals("decision")) {

return e.attributeValue("expr");

}
}
return null;

}

public void build(String sourceName) throws FileNotFoundException,
DocumentException {
SAXReader reader = new SAXReader();

document = reader.read(this.getInputStream(sourceName));// 读取XML文档
elementRoot = document.getRootElement();// 声明根元素
}

// 输出所有的孩子节点名字
public void printAllNodes(ArrayList<XmlNode> xmlNodes) {

for (XmlNode xmlNode : xmlNodes) {

System.out.println("node name=" + xmlNode.getNode().getName());
System.out.println("node FullName=" + xmlNode.getFullName());
System.out.println("node PathName=" + xmlNode.getPathName());
for (Iterator it = xmlNode.getNode().attributeIterator(); it
.hasNext();) {
Attribute attribute = (Attribute) it.next();
System.out.print("Attributes Name=");
System.out.println(attribute.getName());
System.out.print("Attributes Text=");
String text = attribute.getText();
System.out.println(text);
}
System.out.println("============================");

}
}

// 取得所有的孩子节点
public ArrayList<XmlNode> listChildNodes(String sourceName) {

ArrayList<XmlNode> xmlNodes = this.listAllNodes(this.getRoot());

for (XmlNode xmlNode : xmlNodes) {

if (xmlNode.getPathName().equals(sourceName)) {
return this.listAllNodes(xmlNode.getNode());

}
}
return new ArrayList<XmlNode>();
}

// 添加一个节点到集合
private void addNode(ArrayList<XmlNode> xmlNodes, Element root) {
if (root.getNodeType() == 1) {
XmlNode xmlNode = new XmlNode();
String nodeName = root.getName();
xmlNode.setNode(root);
xmlNode.setName(nodeName);
xmlNode.setFullName(nodeName);
xmlNode.setPathName(nodeName);
xmlNodes.add(xmlNode);
}

List<Element> list = root.elements();

for (int i = 0; i < list.size(); i++) {
Element node = (Element) list.get(i);

addNode(xmlNodes, node);
}
}

// 取得所有节点
public ArrayList<XmlNode> listAllNodes(Element root) {

ArrayList<XmlNode> xmlNodes = new ArrayList<XmlNode>();
this.addNode(xmlNodes, root);
return xmlNodes;

}

// 转换成InputStream
public InputStream getInputStream(String sourceName)
throws FileNotFoundException {
URL url = this.getClass().getClassLoader().getResource(sourceName);

FileInputStream fis = new FileInputStream(new File(url.getPath()));

return fis;

}

// 取得开始节点
public Element getRoot() {
return this.elementRoot;
}
}


--------------------------------------------------
XmlUtil.java
package workflow.engine.util;


public class XmlUtil {
private Document document = null;

private Element elementRoot = null;

public String getExpr(InputStream is, String type) throws DocumentException {

this.build(is);

List<Element> list = this.getRoot().elements();

for (Element e : list) {

if (e.getName().equals(type)) {

return e.attributeValue("expr");

}
}
return null;

}

public String getExprFromPattern(String expr) {

Pattern pattern = Pattern.compile("^(\\#\\{)(\\D+?)(\\})$");

Matcher matcher = pattern.matcher(expr);

if (matcher.find())

{
return matcher.group(2);

}
return expr;

}

public String getExpr() {

List<Element> list = this.getRoot().elements();

for (Element e : list) {

if (e.getName().equals("decision")) {

return e.attributeValue("expr");

}
}
return null;

}

public void build(InputStream is) throws DocumentException {
SAXReader reader = new SAXReader();

document = reader.read(is);// 读取XML文档
elementRoot = document.getRootElement();// 声明根元素
}

public void build(String sourceName) throws FileNotFoundException,
DocumentException {
SAXReader reader = new SAXReader();

document = reader.read(this.getInputStream(sourceName));// 读取XML文档
elementRoot = document.getRootElement();// 声明根元素
}

// 转换成InputStream
public InputStream getInputStream(String sourceName)
throws FileNotFoundException {
URL url = this.getClass().getClassLoader().getResource(sourceName);

FileInputStream fis = new FileInputStream(new File(url.getPath()));

return fis;

}

// 取得开始节点
public Element getRoot() {
return this.elementRoot;
}
}

你可能感兴趣的:(java,xml,.net,workflow)