Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. try {
  2. final ByteArrayOutputStream bos = new ByteArrayOutputStream();
  3. final XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(bos);
  4. writer.writeStartDocument("UTF-8", "1.0");
  5. writer.writeStartElement("a");
  6. writer.writeDefaultNamespace(NS);
  7. writer.writeStartElement("b");
  8. writer.writeStartElement("c");
  9. writer.writeCharacters("d");
  10. writer.writeEndElement();
  11. writer.writeStartElement("e");
  12. writer.writeStartElement("f");
  13. writer.writeCharacters("g");
  14. writer.writeEndElement();
  15. writer.writeEndElement();
  16. writer.writeEndElement();
  17. writer.writeEndElement();
  18. //System.out.println(new String(bos.toByteArray(), StandardCharsets.UTF_8));
  19. return new ByteArrayInputStream(bos.toByteArray());
  20. } catch (Exception e) {
  21. throw new RuntimeException(e);
  22. }
  23.  
  24. try {
  25. final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
  26. final Node policyClause = doc
  27. .appendChild(doc.createElementNS(NS, "a"))
  28. .appendChild(doc.createElementNS(NS, "b"));
  29. policyClause
  30. .appendChild(doc.createElementNS(NS, "c"))
  31. .setTextContent("d");
  32. policyClause
  33. .appendChild(doc.createElementNS(NS, "e"))
  34. .appendChild(doc.createElementNS(NS, "f"))
  35. .setTextContent("g");
  36. final ByteArrayOutputStream bos = new ByteArrayOutputStream();
  37. TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), new StreamResult(bos));
  38. //System.out.println(new String(bos.toByteArray(), StandardCharsets.UTF_8));
  39. return new ByteArrayInputStream(bos.toByteArray());
  40. } catch (Exception e) {
  41. throw new RuntimeException(e);
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement