简体   繁体   English

如何在Java中清除XML标头?

[英]How I can clean the header of a XML in java?

I want clean the header of a XML in JAVA like: 我想在JAVA中清理XML的标头,例如:

<?xml version="1.0" encoding="utf-8"?>
<Document xmlns="xxxx" xmlns:xsi="yyyy" xsi:schemaLocation="zzzz">
...

to

<?xml version="1.0" encoding="utf-8"?>
<Document>
...

I have a documentBuilder with the xml 我有一个带有xml的documentBuilder

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
org.w3c.dom.Document document = builder.parse(new InputSource(new  StringReader(strXmlContent)));

and now I dont know how I can do ti, any help?? 现在我不知道该怎么办,有什么帮助吗?

Try this: 尝试这个:

NodeList nodes = document.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
    String id = nodes.item(i).getNodeValue();
    Element el = ((Attr) nodes.item(i)).getOwnerElement();
    el.removeAttribute(id);
}

To print it back: 要打印回来:

    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
    StreamResult result = new StreamResult( new StringWriter() );
    DOMSource source = new DOMSource( document );
    transformer.transform( source, result );

    String xmlString = result.getWriter().toString();
    System.out.println( xmlString );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM