I need to convert this file to java. I'm taking the file as a document and need to edit it by making a change to the namespace.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.oorsprong.org/websamples.countryinfo">
<soapenv:Header/>
<soapenv:Body>
<web:CapitalCity>
<web:sCountryISOCode>...</web:sCountryISOCode>
</web:CapitalCity>
</soapenv:Body>
</soapenv:Envelope>`
to:
<Envelope>
<Header/>
<Body>
<CapitalCity>
<sCountryISOCode>...</sCountryISOCode>
</CapitalCity>
</Body>
</Envelope>
How can I do?
my code:
String xml="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://www.oorsprong.org/websamples.countryinfo\">\r\n" +
" <soapenv:Header/>\r\n" +
" <soapenv:Body>\r\n" +
" <web:CapitalCity>\r\n" +
" <web:sCountryISOCode>...</web:sCountryISOCode>\r\n" +
" </web:CapitalCity>\r\n" +
" </soapenv:Body>\r\n" +
" </soapenv:Envelope>";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document doc = dbf.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
StringWriter buf = new StringWriter();
Transformer xform = TransformerFactory.newInstance().newTransformer();
xform.transform(new DOMSource(doc), new StreamResult(buf));
String output = buf.getBuffer().toString().replaceAll("<[^/](.+?):", "<");
String output2=output.replaceAll("</(.+?):", "</");
System.out.println(output2);
I want to do this without using regex, is this possible?
I made using xslt:
public String envelopeDocument_2_XSL(Document data) throws IOException, TransformerFactoryConfigurationError, TransformerException {
URL xsltURL = getClass().getClassLoader().getResource("XSLT.xsl");
Source stylesource = new StreamSource(xsltURL.openStream(), xsltURL.toExternalForm());
Transformer transformer = TransformerFactory.newInstance().newTransformer(stylesource);
StringWriter stringWriter = new StringWriter();
transformer.transform(new DOMSource(data), new StreamResult(stringWriter));
return stringWriter.toString();
}
When the xml document above is given to this function, it removes the namespaces.
XSLT code that removes namespaces:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="node() | @*" />
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:template>
<xsl:template match="comment()[contains(., '')]" />
</xsl:stylesheet>