Last active
October 21, 2015 14:21
-
-
Save Crydust/2b431bcb8efde42c78f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.nio.charset.StandardCharsets; | |
import javax.xml.parsers.DocumentBuilder; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import javax.xml.parsers.ParserConfigurationException; | |
import javax.xml.transform.OutputKeys; | |
import javax.xml.transform.Result; | |
import javax.xml.transform.Source; | |
import javax.xml.transform.Transformer; | |
import javax.xml.transform.TransformerConfigurationException; | |
import javax.xml.transform.TransformerException; | |
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.XPathExpression; | |
import javax.xml.xpath.XPathExpressionException; | |
import javax.xml.xpath.XPathFactory; | |
import org.w3c.dom.Document; | |
import org.w3c.dom.Node; | |
import org.w3c.dom.NodeList; | |
import org.xml.sax.SAXException; | |
public final class XmlUtil { | |
private XmlUtil() { | |
// NOOP | |
} | |
public static void main(String[] args) throws Exception { | |
// usage | |
final byte[] input = "<root>\n <node>old 木</node>\n</root>" | |
.getBytes(StandardCharsets.US_ASCII); | |
final Document doc = documentFromByteArray(input); | |
final XPath xpath = createXpath(); | |
final String old = readString(doc, xpath, "/root/node/text()"); | |
if ("old \u6728".equals(old)) { | |
writeString(doc, xpath, "/root/node/text()", "new \u6728"); | |
} | |
final byte[] output = documentToByteArray(doc); | |
//final String nl = System.getProperty("line.separator"); | |
//assert ("<root>" + nl | |
// + " <node>new 木</node>" + nl | |
// + "</root>" + nl) | |
// .equals(new String(output, StandardCharsets.US_ASCII)); | |
System.out.println(new String(output, StandardCharsets.US_ASCII)); | |
} | |
public static Document documentFromByteArray(final byte[] bytes) throws SAXException, IOException, ParserConfigurationException { | |
final Document doc = documentFromInputStream(new ByteArrayInputStream(bytes)); | |
return doc; | |
} | |
public static Document documentFromInputStream(final InputStream is) throws SAXException, IOException, ParserConfigurationException { | |
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
final DocumentBuilder builder = factory.newDocumentBuilder(); | |
final Document doc = builder.parse(is); | |
doc.getDocumentElement().normalize(); | |
return doc; | |
} | |
public static byte[] documentToByteArray(final Document doc) throws TransformerConfigurationException, TransformerException { | |
final Source source = new DOMSource(doc); | |
final ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
final Result result = new StreamResult(out); | |
final TransformerFactory factory = TransformerFactory.newInstance(); | |
final Transformer transformer = factory.newTransformer(); | |
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); | |
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); | |
transformer.setOutputProperty(OutputKeys.ENCODING, "US-ASCII"); | |
transformer.setOutputProperty(OutputKeys.METHOD, "xml"); | |
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); | |
transformer.transform(source, result); | |
return out.toByteArray(); | |
} | |
public static XPath createXpath() { | |
final XPathFactory xPathfactory = XPathFactory.newInstance(); | |
final XPath xpath = xPathfactory.newXPath(); | |
return xpath; | |
} | |
public static String readString(final Node node, final XPath xpath, final String xpathExpression) throws XPathExpressionException { | |
final XPathExpression expr = xpath.compile(xpathExpression); | |
final String s = (String) expr.evaluate(node, XPathConstants.STRING); | |
return s; | |
} | |
public static void writeString(final Node node, final XPath xpath, final String xpathExpression, final String value) throws XPathExpressionException { | |
final XPathExpression expr = xpath.compile(xpathExpression); | |
final Node n = (Node) expr.evaluate(node, XPathConstants.NODE); | |
n.setNodeValue(value); | |
} | |
public static NodeList findNodes(final Node node, final XPath xpath, final String xpathExpression) throws XPathExpressionException { | |
final XPathExpression expr = xpath.compile(xpathExpression); | |
final NodeList list = (NodeList) expr.evaluate(node, XPathConstants.NODESET); | |
return list; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment