package com.huayingsoft.util;
import java.io.File;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Attr;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class XMLHelper {
private Document doc;
public XMLHelper() {
}
/**
* 判断文件扩展名
*
* @param fileNameStr
* 文件名
* @param extendName
* 扩展名
*/
public boolean estimateExtend(String fileNameStr, String extendName) {
int pos = fileNameStr.trim().lastIndexOf(".");
if (pos == -1 || pos == 0 || pos == fileNameStr.trim().length() - 1)
return false;
String extendStr = fileNameStr.trim().substring(pos + 1);
if (extendStr.equalsIgnoreCase(extendName))
return true;
else
return false;
}
/**
* 更改文件扩展名
*
* @param fileNameStr
* 文件名
* @param extendName
* 扩展名
*/
private String changeExtend(String fileNameStr, String extendName) {
int pos = fileNameStr.trim().lastIndexOf(".");
if (pos == -1 || pos == 0)
return fileNameStr;
fileNameStr = fileNameStr.trim().substring(0, pos + 1) + extendName;
return fileNameStr;
}
public String readXmlThreeLayer(String xmlFile, String Xpath,
boolean hasAttr, String[] attrNames) throws Exception {
try {
StringBuilder sb = new StringBuilder();
File f = new File(xmlFile);
if (f.exists()) {
// XmlNodeList list = null;
NodeList list = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory
.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
this.doc = db.parse(f);
String attr = "";
// this.doc = new XmlDocument();
// this.doc.Load(xmlFile);
if (Xpath != null || Xpath.length() > 0) {
XPath xpath = XPathFactory.newInstance().newXPath();
list = (NodeList) xpath.evaluate(Xpath, this.doc,
XPathConstants.NODESET);
// list = this.doc.SelectSingleNode(Xpath).ChildNodes;
} else {
list = this.doc.getDocumentElement().getChildNodes();
}
int lengthTop = list.getLength();
for (int i = 0; i < lengthTop; i++) {
int lengthTwo = list.item(i).getChildNodes().getLength();
for (int j = 0; j < lengthTwo; j++) {
int lengthThree = list.item(i).getChildNodes().item(j)
.getChildNodes().getLength();
if (lengthThree > 1) {
for (int k = 0; k < lengthThree; k++) {
String node = list.item(i).getChildNodes()
.item(j).getChildNodes().item(k)
.getNodeValue();
if (hasAttr)
attr = this.readAttribute(list.item(i)
.getChildNodes().item(j)
.getChildNodes().item(k)
.getAttributes(), attrNames);
if (k != lengthThree - 1) {
// sb.AppendFormat("{0}{1}$", node, attr);
sb.append(node + attr + "$");
} else {
// sb.AppendFormat("{0}{1}", node, attr);
sb.append(node + attr);
}
}
} else if (lengthThree == 1) {
// sb.AppendFormat("{0}",
// list.Item(i).ChildNodes.Item(j).ChildNodes.Item(0).InnerText);
sb.append(list.item(i).getChildNodes().item(j)
.getChildNodes().item(0).getNodeValue());
} else {
// sb.AppendFormat("{0}", list.Item(i).InnerText);
sb.append(list.item(i).getNodeValue());
}
if (hasAttr)
attr = this.readAttribute(list.item(i)
.getChildNodes().item(j).getAttributes(),
attrNames);
if (j != lengthTwo - 1) {
// sb.AppendFormat("{0}#", attr);
sb.append(attr + "#");
} else {
// sb.AppendFormat("{0}", attr);
sb.append(attr);
}
}
if (hasAttr)
attr = this.readAttribute(list.item(i).getAttributes(),
attrNames);
// sb.AppendFormat("{0}|", attr);
sb.append(attr + "|");
}
} else
sb.append("--文件不存在!--");
return sb.toString();
} catch (Exception e) {
throw e;
}
}
public String readXmlThreeLayer(String xmlFile, boolean hasAttr,
String[] attrNames) {
try {
return this.readXmlThreeLayer(xmlFile, "", hasAttr, attrNames);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public String readXml(String xmlFile, String pNodePath, String[] nodeNames,
boolean hasAttr, String[] attrNames) throws Exception {
StringBuilder sb = new StringBuilder();
File f = new File(xmlFile);
try {
if (f.exists()) {
Node root = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory
.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
this.doc = db.parse(f);
// this.doc = new XmlDocument();
// this.doc.Load(xmlFile);
if (pNodePath != null && pNodePath.length() > 0) {
XPath xpath = XPathFactory.newInstance().newXPath();
root = (Node) xpath.evaluate(pNodePath, this.doc,
XPathConstants.NODE);
// root = this.doc.SelectSingleNode(pNodePath);
} else
root = this.doc.getDocumentElement();
if (root != null) {
if (root.hasChildNodes()) {
// sb.AppendFormat("{0}",
// this.readChildNode(root.ChildNodes, nodeNames,
// hasAttr, attrNames));
sb.append(this.readChildNode(root.getChildNodes(),
nodeNames, hasAttr, attrNames));
} else {
// sb.AppendFormat("{0}",root.InnerText);
sb.append(root.getNodeValue());
}
} else
sb.append("--节点不存在--");
} else
sb.append("--文件不存在!--");
} catch (Exception e) {
throw e;
}
return sb.toString();
}
public String readXml(String xmlFile, String pNodePath, String[] nodeNames,
boolean hasAttr) {
try {
return this.readXml(xmlFile, pNodePath, nodeNames, hasAttr, null);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public String readXml(String xmlFile, String pNodePath, boolean hasAttr)
throws Exception {
StringBuilder sb = new StringBuilder();
File f = new File(xmlFile);
try {
if (f.exists()) {
NodeList root = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory
.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
this.doc = db.parse(f);
// this.doc=new XmlDocument();
// this.doc.Load(xmlFile);
if (pNodePath != null && !pNodePath.equals("")) {
if (!pNodePath.trim().equals("%")) {
// sb.AppendFormat("{0}",this.readXml(xmlFile,pNodePath,null,false,null));
XPath xpath = XPathFactory.newInstance().newXPath();
// XmlNode node=this.doc.SelectSingleNode(pNodePath);
Node node = (Node) xpath.evaluate(pNodePath, this.doc,
XPathConstants.NODE);
if (node != null) {
if (node.hasChildNodes()) {
root = node.getChildNodes();
int len = root.getLength();
for (int p = 0; p < len; p++) {
if (root.item(p).hasChildNodes()) {
int tmp = root.item(p).getChildNodes()
.getLength();
for (int k = 0; k < tmp; k++) {
if (k == tmp - 1) {
String attr1 = "";
if (hasAttr)
attr1 = this
.readAttribute(root
.item(p)
.getAttributes());
// sb.AppendFormat("{0}{1}|",root.Item(p).ChildNodes.Item(k).InnerText,attr1);
sb.append(root.item(p)
.getChildN