📄 xmlhandler.java
字号:
package com.zznode.dp.ismg.util;
import org.w3c.dom.*;
import java.util.*;
import javax.xml.parsers.*;
import java.io.*;
import org.apache.xerces.dom.DocumentImpl;
import org.apache.xml.serialize.*;
import java.net.*;
/**
* <p>Title: XML文件读写类</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author 孟祥德
* @version 1.0
*/
public class XmlHandler {
/**
* dom对象
*/
Document document;
/**
* file对象
*/
File file;
/**
* 读模式
*/
public static final int READ_MODE = 1;
/**
* 读写模式
*/
public static final int READ_WRITE_MODE = 2;
public XmlHandler() throws Exception {
document = new DocumentImpl();
}
/**
* 构造函数,构造XML文件dom tree
*
* @param uri 文件名
*/
public XmlHandler(String uri) throws Exception {
this(new File(uri));
}
/**
* 构造函数,构造XML文件dom tree
*
* @param file 文件
*/
public XmlHandler(File file) throws Exception {
// parse document
//org.apache.xerces.parsers.DOMParser parser =new org.apache.xerces.parsers.DOMParser();
//parser.parse(uri);
//document = parser.getDocument();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(file);
}
/**
* 构造函数,构造XML文件dom tree
*
* @param in
*/
public XmlHandler(InputStream in) throws Exception {
// parse document
//org.apache.xerces.parsers.DOMParser parser =new org.apache.xerces.parsers.DOMParser();
//parser.parse(uri);
//document = parser.getDocument();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(in);
}
/**
* 构造函数,构造XML文件dom tree
*
* @param uri 文件名
* @param mode 打开模式
*/
public XmlHandler(String uri, int mode) throws Exception {
if (mode == READ_WRITE_MODE) {
try {
file = new File(uri);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(file);
}
catch (Exception e) {
file = new File(uri);
document = new DocumentImpl();
}
}
else {
file = new File(uri);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(file);
}
}
/**
* 构造函数,构造XML文件dom tree
*
* @param file 文件
* @param mode 打开模式
*/
public XmlHandler(File file, int mode) {
if (mode == READ_WRITE_MODE) {
try {
this.file = file;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(file);
}
catch (Exception e) {
try {
this.file = file;
document = new DocumentImpl();
}
catch (Exception ex) {
ex.printStackTrace(System.err);
}
}
}
else {
try {
this.file = file;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(file);
}
catch (Exception e) {
e.printStackTrace(System.err);
}
}
}
/**
* 构造函数,构造XML文件dom tree
*
* @param url
* @param mode 打开模式
*/
public XmlHandler(URL url, int mode) throws Exception {
this(url.getFile(), mode);
}
/**
* 构造函数,构造XML文件dom tree
*
* @param url 文件名
*/
public XmlHandler(URL url) throws Exception {
URLConnection connection = url.openConnection();
connection.connect();
InputStream in = connection.getInputStream();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(in);
}
/**
* 取document对象
*
* @return document对象
*/
public Document getDocument() {
return document;
}
/**
* 取子元素
*
* @param parent 父节点
* @return 子元素
*/
public Node getChildElement(Node parent) {
if (parent == null)
return null;
NodeList nodeList = parent.getChildNodes();
if (nodeList == null)
return null;
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE)
return node;
}
return null;
}
/**
* 根据节点名取子节点,找到第一个即返回
*
* @param ParentNode 父节点
* @param dn 节点名,多层路径以"."分隔
* @return 子节点
*/
public Node findNode(Node ParentNode, String dn) {
if (ParentNode == null)
return null;
if (dn == null || dn.length() == 0)
return ParentNode;
String NodeName = dn;
String Subdn = null;
boolean isLeafNode = false;
int j = dn.indexOf(".");
if (j != -1) {
NodeName = dn.substring(0, j);
Subdn = dn.substring(j + 1, dn.length());
}
else {
isLeafNode = true;
}
NodeList ChildrenList = ParentNode.getChildNodes();
if (ChildrenList == null)
return null;
Node childNode = null;
for (int i = 0; i < ChildrenList.getLength(); i++) {
childNode = ChildrenList.item(i);
String childNodeName = childNode.getNodeName();
if (childNode.getNodeType() == Node.ELEMENT_NODE &&
childNodeName.equalsIgnoreCase(NodeName)) {
if (isLeafNode) //如果dn是叶子元素,直接返回本次找到的节点
return childNode;
else
return findNode(childNode, Subdn);
} // end if
} // end for-loop
return null;
}
/**
* 根据节点名取子节点,找到第一个即返回
*
* @param ParentNode 父节点
* @param dn 节点名
* @return 子节点
*/
public Node findNode2(Node ParentNode, String dn) {
if (ParentNode == null)
return null;
if (dn == null || dn.length() == 0)
return ParentNode;
String NodeName = dn;
String Subdn = null;
boolean isLeafNode = false;
//int j = dn.indexOf(".");
// if (j != -1) {
// NodeName = dn.substring(0, j);
// Subdn = dn.substring(j + 1, dn.length());
// } else {
// isLeafNode = true;
// }
NodeList ChildrenList = ParentNode.getChildNodes();
if (ChildrenList == null)
return null;
Node childNode = null;
for (int i = 0; i < ChildrenList.getLength(); i++) {
childNode = ChildrenList.item(i);
String childNodeName = childNode.getNodeName();
if (childNode.getNodeType() == Node.ELEMENT_NODE &&
childNodeName.equalsIgnoreCase(NodeName)) {
if (isLeafNode) //如果dn是叶子元素,直接返回本次找到的节点
return childNode;
else
return findNode(childNode, Subdn);
} // end if
} // end for-loop
return null;
}
/**
* 根据节点名找到多个子节点
*
* @param parentNode 父节点
* @param dn 节点名,多层路径以"."分隔
* @return 子节点数组
*/
public Node[] findNodes(Node parentNode, String dn) {
if (parentNode == null)
return null;
Node firstNode = findNode(parentNode, dn);
if (firstNode == null)
return null;
String nodeName = firstNode.getNodeName();
Vector nodes = new Vector();
Node siblingNode = firstNode;
while (siblingNode != null) {
if (siblingNode.getNodeType() == Node.ELEMENT_NODE &&
nodeName.equalsIgnoreCase(siblingNode.getNodeName())) {
nodes.add(siblingNode);
}
siblingNode = siblingNode.getNextSibling();
}
Node[] nodeArray = new Node[nodes.size()];
nodes.copyInto(nodeArray);
return nodeArray;
}
/**
* 从根结点通过属性找到同名节点的指定节点,找到第一个即返回
*
* @param dn 节点名,多层路径以"."分隔
* @param attrName 属性名
* @param attrValue 属性值
* @return 子节点
*/
public Node findNode(String dn, String attrName, String attrValue) {
return findNode(getRootNode(), dn, attrName, attrValue);
}
/**
* 从给定节点通过属性找到同名节点的指定节点,找到第一个即返回
*
* @param parentNode 父节点
* @param dn 节点名,多层路径以"."分隔
* @param attrName 属性名
* @param attrValue 属性值
* @return 子节点
*/
public Node findNode(Node parentNode, String dn, String attrName,
String attrValue) {
if (parentNode == null)
return null;
Node firstNode = findNode(parentNode, dn);
if (firstNode == null)
return null;
String nodeName = firstNode.getNodeName();
Node findNode = null;
Node siblingNode = firstNode;
while (siblingNode != null) {
if (siblingNode.getNodeType() == Node.ELEMENT_NODE &&
nodeName.equalsIgnoreCase(siblingNode.getNodeName())) {
String theAttrValue = ( (Element) siblingNode).getAttribute(attrName);
if (theAttrValue.equals(attrValue)) {
findNode = siblingNode;
break;
}
}
siblingNode = siblingNode.getNextSibling();
}
return findNode;
}
/**
* 找到一个父节点下的多个同名节点
*
* @param parentNode 父节点
* @param dn 节点名,多层路径以"."分隔
* @param tagName 标记名
* @return 子节点数组
*/
public Node[] findNodes(Node parentNode, String dn, String tagName) {
if (parentNode == null)
return null;
String nodeName = dn;
String subdn = null;
boolean isLeafNode = false;
int j = dn.indexOf(".");
if (j != -1) {
nodeName = dn.substring(0, j);
subdn = dn.substring(j + 1, dn.length());
}
else {
isLeafNode = true;
}
Node childNode = null;
if (!dn.equals(tagName)) {
NodeList childrenList = parentNode.getChildNodes();
if (childrenList == null)
return null;
for (int i = 0; i < childrenList.getLength(); i++) {
childNode = childrenList.item(i);
String childNodeName = childNode.getNodeName();
if (childNode.getNodeType() == Node.ELEMENT_NODE &&
childNodeName.equalsIgnoreCase(nodeName)) {
if (isLeafNode) { //如果dn是叶子元素,直接返回本次找到的节点的相应子节点
break;
}
else
return findNodes(childNode, subdn, tagName);
} // end if
} // end for-loop
} //end if
else childNode = parentNode;
if (childNode.hasChildNodes()) {
Vector nodes = new Vector();
NodeList lastNodesList = childNode.getChildNodes();
for (int ii = 0; ii < lastNodesList.getLength(); ii++) {
Node tagNode = lastNodesList.item(ii);
String tagNodeName = tagNode.getNodeName();
if (tagNode.getNodeType() == Node.ELEMENT_NODE &&
tagNodeName.equalsIgnoreCase(tagName)) {
nodes.addElement(tagNode);
}
}
if (nodes.isEmpty())
return null;
else {
Node[] returnNodes = new Node[nodes.size()];
nodes.copyInto(returnNodes);
return returnNodes;
}
}
else
return null;
}
/**
* 取得一个节点的指定属性
*
* @param dn 节点名,多层路径以"."分隔
* @param attrName 属性名
* @return 属性值
*/
public java.lang.String getElementAttr(String dn, String attrName) {
Node node = findNode(getRootNode(), dn);
if (node != null) {
String attrValue = ( (Element) node).getAttribute(attrName);
if (attrValue.length() > 0)
return attrValue;
}
return null;
}
/**
* 取得一个节点的指定属性
*
* @param node 节点
* @param attrName 属性名
* @return 属性值
*/
public java.lang.String getElementAttr(Node node, String attrName) {
if (node != null) {
String attrValue = ( (Element) node).getAttribute(attrName);
if (attrValue.length() > 0)
return attrValue;
}
return null;
}
/**
* 取得给定节点下多个同名节点的指定属性
*
* @param parentNode 父节点
* @param dn 节点名,多层路径以"."分隔
* @param attrName 属性名
* @return 属性值数组
*/
public java.lang.String[] getElementsAttr(Node parentNode, String dn,
String attrName) {
Node[] nodes = findNodes(parentNode, dn);
if (nodes == null)
return null;
int l = nodes.length;
String[] attrs = new String[l];
for (int i = 0; i < l; i++) {
attrs[i] = ( (Element) nodes[i]).getAttribute(attrName);
}
return attrs;
}
/**
* 取得根节点下多个同名节点的指定属性
*
* @param dn 节点名,多层路径以"."分隔
* @param attrName 属性名
* @return 属性值数组
*/
public java.lang.String[] getElementsAttr(String dn, String attrName) {
Node[] nodes = findNodes(getRootNode(), dn);
if (nodes != null && nodes.length > 0) {
int l = nodes.length;
String[] attrs = new String[l];
for (int i = 0; i < l; i++) {
attrs[i] = ( (Element) nodes[i]).getAttribute(attrName);
}
return attrs;
}
else
return null;
}
/**
* 根据一个属性和节点路径找到唯一的节点,取得它的指定属性
*
* @param dn 节点名,多层路径以"."分隔
* @param attrName 已知属性名
* @param attrValue 已知属性值
* @param resultAttrName 属性名
* @return 属性值
*/
public String getElementAttrByAttr(String dn, String attrName,
String attrValue, String resultAttrName) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -