Creating an XML document using namespaces in Java

There are a number of ways of doing this. Just a couple of examples: Using XOM import nu.xom.Document; import nu.xom.Element; public class XomTest { public static void main(String[] args) { XomTest xomTest = new XomTest(); xomTest.testXmlDocumentWithNamespaces(); } private void testXmlDocumentWithNamespaces() { Element root = new Element(“my:example”, “urn:example.namespace”); Document document = new Document(root); Element element = … Read more

Java How to extract a complete XML block

Adding to lwburk’s solution, to convert a DOM Node to string form, you can use a Transformer: private static String nodeToString(Node node) throws TransformerException { StringWriter buf = new StringWriter(); Transformer xform = TransformerFactory.newInstance().newTransformer(); xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, “yes”); xform.transform(new DOMSource(node), new StreamResult(buf)); return(buf.toString()); } Complete example: public static void main(String… args) throws Exception { String xml = … Read more