List of utility methods to do XML String Transform
void | save(Node doc, OutputStream stream, String encoding, boolean indent) save save(doc, stream, encoding, indent, null, null); |
void | signEmbeded(Node doc, String uri, PrivateKey pKey, X509Certificate cert) Firma digitalmente usando la forma "enveloped signature" según el estándar de la W3C (http://www.w3.org/TR/xmldsig-core/). XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM"); Reference ref = fac.newReference(uri, fac.newDigestMethod(DigestMethod.SHA1, null), Collections.singletonList(fac.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null)), null, null); String method = SignatureMethod.RSA_SHA1; if ("DSA".equals(cert.getPublicKey().getAlgorithm())) method = SignatureMethod.DSA_SHA1; else if ("HMAC".equals(cert.getPublicKey().getAlgorithm())) ... |
Document | String2Doc(String InputXMLString) Helper program: Transforms a String to a XML Document. try { DocumentBuilder builder = getDocumentBuilder(); InputSource is = new InputSource(new StringReader(InputXMLString)); is.setEncoding("UTF-8"); return builder.parse(is); } catch (Exception e) { System.out.println("cannot parse following content\\n\\n" + InputXMLString); e.printStackTrace(); ... |
Source | string2Source(String xml) string Source System.out.println("xml:" + xml); return new StreamSource(new StringReader(xml)); |
Node | stringToNode(String s) string To Node StreamSource ss = new StreamSource(new StringReader(s)); DOMResult dr = new DOMResult(); try { Transformer t = TransformerFactory.newInstance().newTransformer(); t.transform(ss, dr); } catch (TransformerException te) { te.printStackTrace(); return null; ... |
Schema | strToSchema(final String strXsd) str To Schema SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); try { return factory.newSchema(new StreamSource(new StringReader(strXsd))); } catch (SAXException e) { e.printStackTrace(); return null; |
Element | toElement(String xml) to Element DOMResult dom = new DOMResult(); try { TransformerFactory.newInstance().newTransformer().transform(new StreamSource(new StringReader(xml)), dom); } catch (Exception e) { return ((Document) dom.getNode()).getDocumentElement(); |
String | toHexString(byte[] array) Returns a hexadecimal string from a given byte array. if (array != null) return DatatypeConverter.printHexBinary(array); else return ""; |
String | toHTML(String xml) to HTML ByteArrayOutputStream baos = new ByteArrayOutputStream(); StreamResult result = new StreamResult(baos); xmlToHTMLTransformer.transform(new StreamSource(new ByteArrayInputStream(xml.getBytes())), result); return baos.toString(); |
String | toJson(String xml) Transform XML to JSON JsonObject rootJson = new JsonObject(); DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = dBuilder.parse(new InputSource(new StringReader(xml))); if (doc.hasChildNodes()) { traverseNode(doc, rootJson, null); Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create(); String json = gson.toJson(rootJson); ... |