/*
* Created by IntelliJ IDEA.
* User: administrator
* Date: 2002-10-15
* Time: 8:40:42
* To change template for new class use
* Code Style | Class Templates options (Tools | IDE Options).
*/
package com.gxl.test;
import org.apache.xpath.XPathAPI;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import java.util.Properties;
public class xmlUtil implements java.io.Serializable {
public xmlUtil() {
}
public static String GetParentAttribute(Node pNode, String attrName) {
try {
NodeList list = XPathAPI.selectNodeList(pNode, "//maininfo/*");
return ((Element) (list.item(0))).getAttribute(attrName);
} catch (Exception e) {
return null;
}
}
public static String GetNodeAttribute(String Attrname, String nodeSelect, Document selectDoc) {
try {
NodeList list = XPathAPI.selectNodeList(selectDoc, nodeSelect);
return ((Element) (list.item(0))).getAttribute(Attrname);
} catch (Exception e) {
return null;
}
}
public static void SetNodeAttribute(String Attrname, String Attrvalue, String nodeSelect, Document selectDoc) {
try {
NodeList list = XPathAPI.selectNodeList(selectDoc, nodeSelect);
((Element) (list.item(0))).setAttribute(Attrname, Attrvalue);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void SetNodeAttribute(String Attrname, String Attrvalue, Node nodeSelect) {
try {
((Element) (nodeSelect)).setAttribute(Attrname, Attrvalue);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Node AddNode(Document StrDoc, Node selNode, String NodeName) {
Node sonNode = StrDoc.createElement(NodeName);
sonNode.appendChild(StrDoc.createTextNode(""));
selNode.appendChild(sonNode);
return sonNode;
}
public static boolean CreateChildNode(Node selNode, Document StrDoc, String infoNum) {
try {
for (int i = 1; i < Integer.parseInt(infoNum); i++) {
String Num = (i + 1) + "";
String nodeattr = "info[@id='" + Num + "']";
Node bNode = XPathAPI.selectSingleNode(selNode, nodeattr);
if (bNode == null) {
Node sonNode = StrDoc.createElement("info");
((Element) sonNode).setAttribute("id", Num);
sonNode.appendChild(StrDoc.createTextNode(""));
selNode.appendChild(sonNode);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return true;
}
public static Node selectNode(String NodeAttribute, Document strDoc) {
try {
//doc = strDoc;//xmlUtil.getDocument(strDoc);
Node sonNode = XPathAPI.selectSingleNode(strDoc, NodeAttribute);
return sonNode;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
public static DocumentBuilder getBuilder() throws ParserConfigurationException {
DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
dFactory.setNamespaceAware(true);
DocumentBuilder builder = dFactory.newDocumentBuilder();
return builder;
}
//get a document from given file
public static Document getDocument(String path) throws Exception {
//BufferedReader fileIn=new BufferedReader(new FileReader(path));
File f = new File(path);
DocumentBuilder builder = getBuilder();
Document doc = builder.parse(f);
return doc;
}
//get a document from InputStream
public static Document getDocument(InputStream in) throws Exception {
DocumentBuilder builder = getBuilder();
Document doc = builder.parse(in);
return doc;
}
//create a empty document
public static Document getNewDoc() throws Exception {
DocumentBuilder builder = getBuilder();
Document doc = builder.newDocument();
return doc;
}
//create a document from given string
public static Document getNewDoc(String xmlStr) {
Document doc = null;
try {
StringReader sr = new StringReader(xmlStr);
InputSource iSrc = new InputSource(sr);
DocumentBuilder builder = getBuilder();
doc = builder.parse(iSrc);
} catch (Exception ex) {
ex.printStackTrace();
}
return doc;
}
//save a document as a file at the given file path
public static void save(Document doc, String filePath) {
try {
if ((doc != null) && (filePath != null) && (filePath.length() > 0)) {
String STRXML = toString(doc); //Spit out DOM as a String
String path = filePath;
writeXml(STRXML, path);
}
} catch (Exception e) {
e.printStackTrace();
}
}
//save a string(xml) in the given file path
public static void writeXml(String STRXML, String path) {
try {
File f = new File(path);
PrintWriter out = new PrintWriter(new FileWriter(f));
out.print(STRXML + "\n");
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//format a document to string
/*public static String toString(Document doc) {
String STRXML = null;
try {
OutputFormat format = new OutputFormat(doc); //Serialize DOM//
format.setEncoding("GB2312");
StringWriter stringOut = new StringWriter(); //Writer will be a String
XMLSerializer serial = new XMLSerializer(stringOut, format);
serial.asDOMSerializer(); // As a DOM Serializer
serial.serialize(doc.getDocumentElement());
STRXML = stringOut.toString(); //Spit out DOM as a String
} catch (Exception e) {
e.printStackTrace();
}
return STRXML;
}
//format a node to string
public static String toString(Node node, Document doc) {
String STRXML = null;
try {
OutputFormat format = new OutputFormat(doc); //Serialize DOM
//OutputFormat format1 = new OutputFormat(); //Serialize DOM
format.setEncoding("GB2312");
StringWriter stringOut = new StringWriter(); //Writer will be a String
XMLSerializer serial = new XMLSerializer(stringOut, format);
//XMLSerializer serial1 = new XMLSerializer();
serial.asDOMSerializer(); // As a DOM Serializer
serial.serialize((Element) node);
STRXML = stringOut.toString(); //Spit out DOM as a String
int a = STRXML.indexOf("?>") + 3;
STRXML = STRXML.substring(a);
} catch (Exception e) {
e.printStackTrace();
}
return STRXML;
}
*/
public static String toString(Document document){
StringWriter buffer = new StringWriter();
try {
TransformerFactory factory = TransformerFactory.newInstance();