简体   繁体   English

如何修改xml文档并将其作为java中的字符串返回?

[英]How to modify an xml document and return it as a string in java?

I have this string containing my request xml.我有这个包含我的请求 xml 的字符串。 I want to delete below "id" tags from this string if they exist and return it back.我想从这个字符串中删除下面的“id”标签(如果它们存在)并将其返回。 Removing "Identity" completely will also work, if that can be done.如果可以的话,完全删除“身份”也将起作用。

To be deleted :
 <Identity>
<id extension="7865232" aan = "BUH"/>
<code/>
</Identity>
<Identity>
<id extension="88769032" aan = "DIH"/>
<code/>
</Identity>

String XMLString= "<request version ="1.0">
<minMatch>
<val>100</val>
</minMatch>
<paramList>
<payLoad display = "Full" type="F">
</payLoad>
<IQC code = "2">
</IQC>
<Control display="Default" type = "D">
</Control>
<member>
<memCode code = "Active"
</memCode>
<id extension="12345" aan="ACC"></id>
<id extension="54321" aan="REQ"></id>
<id extension="554376" aan="PDR"></id>
<id extension="66321" aan="NJQ"></id>
<addr use = "H">
<streetLine>123</streetLine>
<city>POLIS</city>
<state>NY</state>
<postalCode>44321</postalCode>
</addr>
<telecom value = "5543213">
</telecom>
<Person>
<name>
<given>JOHN</given>
<family>BILL</family>
</name>
<GenderCode code ="M" display="Male">
</GenderCode>
<birthime vaue="19651002">
</birthime>
<Identity>
<id extensio="7865232" aan = "BUH"/>
<code/>
</Identity>
<Identity>
<id extensio="88769032" aan = "DIH"/>
<code/>
</Identity>
</Person>
</member>
----
---
</request>"

I am using below code to achieve the same but it's not working and giving me The constructor DOMSource(Document) is undefined.我正在使用下面的代码来实现相同的功能,但它不起作用,并且给我构造函数 DOMSource(Document) 未定义。 I tired converting document into a byte Array but that's also not working.Is there any way i can simply do this in Java 1.8.我厌倦了将文档转换为字节数组,但这也不起作用。有什么办法可以在 Java 1.8 中简单地做到这一点。 Any other approach is also welcome.也欢迎任何其他方法。 Please suggest.Thank you!请推荐。谢谢!

public void removeIds(String xmlString){

Document doc = null;
SAXBuilder saxbuilder = new SAXBuilder();

try{
doc = saxbuilder.build(new StringReader(xmlString));
}catch(JDOMException je){
je.printStackTrace();
}catch(IOException ie){
ie.printStackTrace();
}

Element rootNode = doc.getRootElement();
IteratorIterable<Element> rootChildren=rootNode.getDescendants(new ElementFilter("Person"));

for(Element Person:rootChildren){
 for(Element Identity:Person.getChildren("Identity")){

((Element) doc.getRootElement.getDescendants(new ElementFilter("Identity"))).removeChild("id");

  }
}

try{

TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer;
        
            transformer = tf.newTransformer();

            // transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

            StringWriter writer = new StringWriter();
            transformer.transform(new DOMSource(doc), new StreamResult(writer));
            String output = writer.getBuffer().toString();
            return output;
        } catch (TransformerException e) {
            e.printStackTrace();
        }
        
       
    }

Since you're invoking a transformation anyway, it's much simpler to do all the work in XSLT rather than fiddle about with low-level DOM manipulation.由于您无论如何都在调用转换,因此在 XSLT 中完成所有工作比摆弄低级 DOM 操作要简单得多。

If you use XSLT 3.0 (Saxon) then the stylesheet is simply如果您使用 XSLT 3.0 (Saxon),那么样式表就是

<xsl:transform version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:mode on-no-match="shallow-copy"/>
  <xsl:output indent="yes" encoding="utf-8"/>
  <xsl:template match="id"/>
</xsl:transform>

You can supply the input to this transformation as new StreamSource(new StringReader(xmlString)) -- there's no need to construct a DOM first.您可以将此转换的输入作为new StreamSource(new StringReader(xmlString)) ——无需先构造 DOM。

It can be done in XSLT 1.0 too (ie with Xalan, which comes bundled with the JDK), but is a bit more verbose.它也可以在 XSLT 1.0 中完成(即使用 Xalan,它与 JDK 捆绑在一起),但有点冗长。

Here is solution using XPath API.这是使用 XPath API 的解决方案。 In this example all elements "id" inside elements "Identity" are removed (XPath expression "//Identity/id").在此示例中,元素“Identity”中的所有元素“id”都被删除(XPath 表达式“//Identity/id”)。 If you want to remove all id elements, change it to "//id".如果要删除所有 id 元素,请将其更改为“//id”。

import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.InputSource;

// function for XML serialization
public static String serialize(Document document) {
        removeWhitespaces(document.getDocumentElement());
        try (StringWriter writer = new StringWriter()) {
            StreamResult result = new StreamResult(writer);
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            transformer.transform(new DOMSource(document), result);
            return writer.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    
    // helper function for stripping whitespaces
    public static void removeWhitespaces(Element element) {
        NodeList children = element.getChildNodes();
        for (int i = children.getLength() - 1; i >= 0; i--) {
            Node child = children.item(i);
            if (child instanceof Text
                && ((Text) child).getData().trim().isEmpty()) {
                element.removeChild(child);
            } else if (child instanceof Element) {
                removeWhitespaces((Element) child);
            }
        }
    }

Main code:主要代码:

    // parse XML
    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = db.parse(new InputSource(new StringReader(xml)));
    
   // find id elements
    XPathExpression xpath = XPathFactory.newInstance().newXPath().compile("//Identity/id");
    NodeList nodes = (NodeList)xpath.evaluate(doc, XPathConstants.NODESET);

    // remove found elements
    for(int i = 0; i < nodes.getLength(); i ++) {
        Node node = nodes.item(i);
        node.getParentNode().removeChild(node);
    }
    
    // serialize and output document
    String result = serialize(doc);
    System.out.println(result);

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

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