How to create a XML object from String in Java?

If you can create a string xml you can easily transform it to the xml document object e.g. – String xmlString = “<?xml version=\”1.0\” encoding=\”utf-8\”?><a><b></b><c></c></a>”; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; try { builder = factory.newDocumentBuilder(); Document document = builder.parse(new InputSource(new StringReader(xmlString))); } catch (Exception e) { e.printStackTrace(); } You can use the document object … Read more

XML shredding via XSLT in Java

Here is a generic solution as requested: <xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”> <xsl:output omit-xml-declaration=”yes” indent=”yes”/> <xsl:strip-space elements=”*”/> <xsl:param name=”pLeafNodes” select=”//Level-4″/> <xsl:template match=”https://stackoverflow.com/”> <t> <xsl:call-template name=”StructRepro”/> </t> </xsl:template> <xsl:template name=”StructRepro”> <xsl:param name=”pLeaves” select=”$pLeafNodes”/> <xsl:for-each select=”$pLeaves”> <xsl:apply-templates mode=”build” select=”/*”> <xsl:with-param name=”pChild” select=”.”/> <xsl:with-param name=”pLeaves” select=”$pLeaves”/> </xsl:apply-templates> </xsl:for-each> </xsl:template> <xsl:template mode=”build” match=”node()|@*”> <xsl:param name=”pChild”/> <xsl:param name=”pLeaves”/> <xsl:copy> <xsl:apply-templates mode=”build” select=”@*”/> … Read more

Default XML namespace, JDOM, and XPath

XPath 1.0 doesn’t support the concept of a default namespace (XPath 2.0 does). Any unprefixed tag is always assumed to be part of the no-name namespace. When using XPath 1.0 you need something like this: public static void main(String args[]) throws Exception { SAXBuilder builder = new SAXBuilder(); Document d = builder.build(“xpath.xml”); XPath xpath = … Read more