我如何在 java 中转换此 xml 文档?
How can I this xml document transform in java?
我需要将此文件转换为 java。
我将文件作为文档,需要通过更改命名空间来编辑它。
<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>`
至:
<Envelope>
<Header/>
<Body>
<CapitalCity>
<sCountryISOCode>...</sCountryISOCode>
</CapitalCity>
</Body>
</Envelope>
我该怎么办?
我的代码:
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);
我想在不使用正则表达式的情况下执行此操作,这可能吗?
应用 XSLT 转换
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/XSL/Transform">
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:transform>
这只需要 XSLT 1.0,因此您可以使用 JDK.
附带的内置 XSLT 转换器来完成此操作
实际上,由于您似乎不喜欢名称空间前缀,因此可以简化为:
<transform version="1.0" xmlns="http://www.w3.org/XSL/Transform">
<template match="*">
<element name="{local-name()}">
<apply-templates/>
</element>
</template>
</transform>
我使用 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();
}
将上述 xml 文档提供给此函数时,它会删除命名空间。
删除名称空间的 XSLT 代码:
<?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>
我需要将此文件转换为 java。 我将文件作为文档,需要通过更改命名空间来编辑它。
<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>`
至:
<Envelope>
<Header/>
<Body>
<CapitalCity>
<sCountryISOCode>...</sCountryISOCode>
</CapitalCity>
</Body>
</Envelope>
我该怎么办?
我的代码:
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);
我想在不使用正则表达式的情况下执行此操作,这可能吗?
应用 XSLT 转换
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/XSL/Transform">
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:transform>
这只需要 XSLT 1.0,因此您可以使用 JDK.
附带的内置 XSLT 转换器来完成此操作实际上,由于您似乎不喜欢名称空间前缀,因此可以简化为:
<transform version="1.0" xmlns="http://www.w3.org/XSL/Transform">
<template match="*">
<element name="{local-name()}">
<apply-templates/>
</element>
</template>
</transform>
我使用 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();
}
将上述 xml 文档提供给此函数时,它会删除命名空间。
删除名称空间的 XSLT 代码:
<?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>