Java tutorial
//package com.java2s; import java.io.ByteArrayInputStream; import java.io.IOException; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import javax.xml.bind.DatatypeConverter; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; 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.w3c.dom.Text; import org.xml.sax.SAXException; public class Main { private final static Charset CHARSET_UTF8 = Charset.forName("UTF-8"); /** * * @param xmlContent * @param expression * @return * @throws SAXException * @throws IOException * @throws ParserConfigurationException * @throws XPathExpressionException */ public static List<String> getHexBinaryValues(String xmlContent, String expression) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException { return getHexBinaryValues(xmlContent, CHARSET_UTF8, expression); } /** * * @param xmlContent * @param charset * @param expression * @return * @throws SAXException * @throws IOException * @throws ParserConfigurationException * @throws XPathExpressionException */ public static List<String> getHexBinaryValues(String xmlContent, Charset charset, String expression) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException { List<String> valueList = new ArrayList<String>(); NodeList nodeList = getNodeList(xmlContent, expression); int length = nodeList.getLength(); for (int seq = 0; seq < length; seq++) { Node node = nodeList.item(seq); if (node.getNodeType() == Node.ELEMENT_NODE) { NodeList childNodeList = node.getChildNodes(); StringBuilder sBuilder = new StringBuilder(); for (int i = 0; i < childNodeList.getLength(); i++) { Node childNode = childNodeList.item(i); if (childNode.getNodeType() == Node.TEXT_NODE) { sBuilder.append(((Text) childNode).getNodeValue()); } } valueList.add(printHexBinary(sBuilder.toString(), charset)); } else if (node.getNodeType() == Node.ATTRIBUTE_NODE) { valueList.add(node.getNodeValue()); } } return valueList; } private static NodeList getNodeList(Document document, String expression) throws XPathExpressionException { XPath xpath = XPathFactory.newInstance().newXPath(); NodeList nodeList = (NodeList) xpath.evaluate(expression, document.getDocumentElement(), XPathConstants.NODESET); return nodeList; } private static NodeList getNodeList(String xmlContent, String expression) throws XPathExpressionException, SAXException, IOException, ParserConfigurationException { Document document = parse(xmlContent, CHARSET_UTF8); XPath xpath = XPathFactory.newInstance().newXPath(); NodeList nodeList = (NodeList) xpath.evaluate(expression, document.getDocumentElement(), XPathConstants.NODESET); return nodeList; } /** * * @param content * @return */ public static String printHexBinary(String content) { return printHexBinary(content, CHARSET_UTF8); } /** * * @param content * @param charset * @return */ public static String printHexBinary(String content, Charset charset) { String converted = content; if (content != null && !content.isEmpty()) { converted = DatatypeConverter.printHexBinary(content.getBytes(charset)); } return converted; } /** * * @param array * @return */ public static String[] printHexBinary(String[] array) { return printHexBinary(array, CHARSET_UTF8); } /** * * @param array * @param charset * @return */ public static String[] printHexBinary(String[] array, Charset charset) { if (array == null || array.length == 0) { return array; } String[] convertedArray = new String[array.length]; for (int i = 0; i < array.length; i++) { convertedArray[i] = printHexBinary(array[i], charset); } return convertedArray; } /** * * @param xmlContent * @param charset * @return * @throws SAXException * @throws IOException * @throws ParserConfigurationException */ private static Document parse(String xmlContent, Charset charset) throws SAXException, IOException, ParserConfigurationException { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(false); documentBuilderFactory.setValidating(false); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(new ByteArrayInputStream(xmlContent.getBytes(charset))); return document; } }