📄 xmlprocessor.java
字号:
package advdb;
import java.io.*;
import java.util.*;
import org.dom4j.*;
import org.dom4j.io.*;
import advdb.dao.*;
public class XMLProcessor
{
private final String DEFAULT_TABLE_NAME = "myxmldoc";
private Document doc;
private SQLServerDatabaseDAO dao;
private int nid = 0;
private String tableName; // if the table name is not provided, we
// would use the default name
public XMLProcessor() throws Exception
{
tableName = DEFAULT_TABLE_NAME;
dao = new SQLServerDatabaseDAO();
}
public void process(String path) throws Exception
{
process(path, parseName(new File(path).getName()));
}
public void process(String path, String tname) throws Exception
{
XMLNode node = this.loadXMLFromFile(path);
if (tname!=null && !"".equals(tname)) tableName = tname;
this.dao.buildTable(tableName);
process(node);
}
protected XMLNode loadXMLFromFile(String path) throws Exception
{
XMLNode node = loadXMLFromStream(new FileInputStream(path));
return node;
}
protected XMLNode loadXMLFromStream(InputStream stream) throws DocumentException
{
SAXReader reader = new SAXReader();
Element root;
doc = reader.read(stream);
root = doc.getRootElement();
return buildXMLTreeFromDOM(0, root);
}
protected XMLNode loadXMLFromDatabase(String tname) throws Exception
{
HashMap<Integer, Node> hash = dao.loadXMLNodes(tname);
for (Integer i:hash.keySet()) {
if (!i.equals(0)) { // root has no parent
Node curr = hash.get(i);
XMLNode parent = (XMLNode)hash.get(curr.getParentId());
if (curr.getType() == Node.NodeTypeElement) {
Vector<XMLNode> children = parent.getChildren();
if (children == null) new Vector<XMLNode>();
children.add((XMLNode)curr);
parent.setChildren(children);
} else {
Vector<XMLAttribute> atts = parent.getAttributes();
if (atts == null) new Vector<XMLAttribute>();
atts.add((XMLAttribute)curr);
parent.setAttributes(atts);
}
}
}
return (XMLNode)hash.get(0);
}
protected XMLNode buildXMLTreeFromDOM(int parent, Element node) {
XMLNode rtn = new XMLNode();
rtn.setType(Node.NodeTypeElement);
rtn.setId(nid++);
rtn.setParentId(parent);
rtn.setName(node.getName());
rtn.setValue(node.getTextTrim());
Vector<XMLAttribute> atts = new Vector<XMLAttribute>();
for (Iterator<Attribute> i=node.attributeIterator();
i.hasNext(); ) {
Attribute att = i.next();
atts.add(new XMLAttribute(
nid++,
att.getName(),
Node.NodeTypeAttribute,
-1, // Attribute has no order
att.getStringValue(),
parent));
}
rtn.setAttributes(atts);
Vector<XMLNode> children = new Vector<XMLNode>();
int order = 1;
for (Iterator<Element> i = node.elementIterator(); i.hasNext();) {
Element e = i.next();
XMLNode child = buildXMLTreeFromDOM(rtn.getId(), e);
child.setOrder(order++);
children.add(child);
}
rtn.setChildren(children);
return rtn;
}
protected void process(XMLNode node) throws Exception
{
dao.saveXMLNode(tableName, node);
for (XMLAttribute att : node.getAttributes()) {
dao.saveXMLNode(tableName, att);
}
for (XMLNode n : node.getChildren()) {
process(n);
}
}
private String parseName(String fn)
{
int pos=fn.indexOf('.');
return fn.substring(0, pos);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -