我想从以下xml字符串中删除xmlns
属性。我写了一个java
程序,但不确定它是否做了需要在这里完成的工作。
如何删除xmlns
属性并获取修改后的xml字符串?
输入XML字符串:
<?xml version="1.0" encoding="UTF-8"?>
<Payment xmlns="http://api.com/schema/store/1.0">
<Store>abc</Store>
</Payment>
预期的XML输出字符串:
<?xml version="1.0" encoding="UTF-8"?>
<Payment>
<Store>abc</Store>
</Payment>
Java类:
public class XPathUtils {
public static void main(String[] args) {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Payment xmlns=\"http://api.com/schema/store/1.0\"><Store>abc</Store></Payment>";
String afterNsRemoval = removeNameSpace(xml);
System.out.println("afterNsRemoval = " + afterNsRemoval);
}
public static String removeNameSpace(String xml) {
try {
System.out.println("before xml = " + xml);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource inputSource = new InputSource(new StringReader(xml));
Document xmlDoc = builder.parse(inputSource);
Node root = xmlDoc.getDocumentElement();
NodeList rootchildren = root.getChildNodes();
Element newroot = xmlDoc.createElement(root.getNodeName());
for (int i = 0; i < rootchildren.getLength(); i++) {
newroot.appendChild(rootchildren.item(i).cloneNode(true));
}
xmlDoc.replaceChild(newroot, root);
return xmlDoc.toString();
} catch (Exception e) {
System.out.println("Could not parse message as xml: " + e.getMessage());
}
return "";
}
}
输出:
before xml = <?xml version="1.0" encoding="UTF-8"?><Payment xmlns="http://api.com/schema/store/1.0"><Store>abc</Store></Payment>
afterNsRemoval = [#document: null]
答案 0 :(得分:3)
你需要一个变压器。检查以下修改后的方法:
public static String removeNameSpace(String xml) {
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty( OutputKeys.METHOD, "xml" );
transformer.setOutputProperty( OutputKeys.INDENT, "false" );
System.out.println("before xml = " + xml);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource inputSource = new InputSource(new StringReader(xml));
Document xmlDoc = builder.parse(inputSource);
Node root = xmlDoc.getDocumentElement();
NodeList rootchildren = root.getChildNodes();
Element newroot = xmlDoc.createElement(root.getNodeName());
for (int i = 0; i < rootchildren.getLength(); i++) {
newroot.appendChild(rootchildren.item(i).cloneNode(true));
}
xmlDoc.replaceChild(newroot, root);
DOMSource requestXMLSource = new DOMSource( xmlDoc.getDocumentElement() );
StringWriter requestXMLStringWriter = new StringWriter();
StreamResult requestXMLStreamResult = new StreamResult( requestXMLStringWriter );
transformer.transform( requestXMLSource, requestXMLStreamResult );
String modifiedRequestXML = requestXMLStringWriter.toString();
return modifiedRequestXML;
} catch (Exception e) {
System.out.println("Could not parse message as xml: " + e.getMessage());
}
return "";
}
输出:
before xml = <?xml version="1.0" encoding="UTF-8"?><Payment xmlns="http://api.com/schema/store/1.0"><Store>abc</Store></Payment>
afterNsRemoval = <?xml version="1.0" encoding="UTF-8"?><Payment><Store>abc</Store></Payment>
答案 1 :(得分:1)
除了上一个操作之外,你已经做好了一切:document.toString()
给你错误的结果,因为它是生成Document
对象的字符串xml表示的错误方法。请参阅此answer,它包含此方法的正确实现:
public static String toString(Document doc) {
try {
StringWriter sw = new StringWriter();
TransformerFactory tf = TransformerFactory.newInstance();
...
答案 2 :(得分:1)
这是使用XPath和vtd-xml(我是作者)的代码...
import com.ximpleware.*;
import java.io.*;
public class removeAttrNode {
public static void main(String[] s) throws VTDException, Exception{
VTDGen vg = new VTDGen(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Payment xmlns=\"http://api.com/schema/store/1.0\"><Store>abc</Store></Payment>";
vg.setDoc(xml.getBytes());
vg.parse(false); // turn off namespace awareness so that
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
XMLModifier xm = new XMLModifier(vn);
ap.selectXPath("//@xmlns");
int i=0;
while((i=ap.evalXPath())!=-1){
xm.remove();
}
xm.output(baos);
System.out.println(baos.toString());
}
}
答案 3 :(得分:0)
考虑XSLT,因为删除声明/未声明的命名空间是一项常规任务。在这里,不需要第三方模块,因为基础Java配备了XSLT 1.0处理器。此外,当XSLT负责转换时,不会处理循环或XML标记/属性重新创建。
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class XPathUtils {
public static void main(String[] args) {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Payment xmlns=\"http://api.com/schema/store/1.0\"><Store>abc</Store></Payment>";
String afterNsRemoval = removeNameSpace(xml);
System.out.println("afterNsRemoval = " + afterNsRemoval);
}
public static String removeNameSpace(String xml) {
try{
String xslStr = String.join("\n",
"<xsl:transform xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">",
"<xsl:output version=\"1.0\" encoding=\"UTF-8\" indent=\"no\"/>",
"<xsl:strip-space elements=\"*\"/>",
" <xsl:template match=\"@*|node()\">",
" <xsl:element name=\"{local-name()}\">",
" <xsl:apply-templates select=\"@*|node()\"/>",
" </xsl:element>",
" </xsl:template>",
" <xsl:template match=\"text()\">",
" <xsl:copy/>",
" </xsl:template>",
"</xsl:transform>");
// Parse XML and Build Document
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
Document doc = db.parse (is);
// Parse XSLT and Configure Transformer
Source xslt = new StreamSource(new StringReader(xslStr));
Transformer tf = TransformerFactory.newInstance().newTransformer(xslt);
// Output Result to String
DOMSource source = new DOMSource(doc);
StringWriter outWriter = new StringWriter();
StreamResult strresult = new StreamResult( outWriter );
tf.transform(source, strresult);
StringBuffer sb = outWriter.getBuffer();
String finalstring = sb.toString();
return(finalstring);
} catch (Exception e) {
System.out.println("Could not parse message as xml: " + e.getMessage());
}
return "";
}
}
答案 4 :(得分:0)
您应该获得当前ELEMENT_NODE和ownerDoc.renameNode(node,null,node.getLocalName())的getOwnerDocument()。 您还应该为需要的所有属性ownerDoc.renameNode(subNode,null,subNode.getLocalName()),为不需要的属性删除removeNamedItemNS。