Java - 从动态XML中删除子节点

时间:2018-06-14 07:45:48

标签: java xml dom removechild xmlnode

我想在Base64中删除包含PDF的 XML节点。这是一个例子:

<?xml version="1.0" encoding="UTF-8"?>
<getResult>
   <id>null</id>
   <pdf>ioje98fh23fjkiwf72322342</pdf>
</getResult>

首先,我将String中的XML转换为Document,但结果为null。这是我的代码:

DocumentBuilder dbf = null;
Document doc = null;

try {
    dbf = DocumentBuilderFactory.newInstance().newDocumentBuilder();

    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader("<getResult><id>null</id><pdf>ioje98fh23fjkiwf72322342</pdf></getResult>"));

    doc = dbf.parse(is);

    NodeList children = doc. getChildNodes();

    for (int i = 0; i < children.getLength(); i++) {

        Node currentChild = children.item(i);

        System.out.println(currentChild);

    }

} catch (Exception e) {
    System.out.println(e.getMessage().toString());
}

结果始终为:[getResult: null]

考虑到主节点可以变化但结构不同,我如何获得PDF节点?

2 个答案:

答案 0 :(得分:2)

您可以使用以检索数据。

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.w3c.dom.CharacterData;

public class LabFour {
    public static void main(String[] args) {
        DocumentBuilder dbf = null;
        Document doc = null;

        try {
            dbf = DocumentBuilderFactory.newInstance().newDocumentBuilder();

            InputSource is = new InputSource();
            is.setCharacterStream(
                    new StringReader("<getResult><id>null</id><pdf>ioje98fh23fjkiwf72322342</pdf></getResult>"));
            doc = dbf.parse(is);

            NodeList nodes = doc.getElementsByTagName("getResult");

            for (int i = 0; i < nodes.getLength(); i++) {
                Element element = (Element) nodes.item(i);

                NodeList name = element.getElementsByTagName("id");
                Element line = (Element) name.item(0);
                System.out.println("id: " + getCharacterDataFromElement(line));

                NodeList pdf = element.getElementsByTagName("pdf");
                line = (Element) title.item(0);
                System.out.println("pdf: " + getCharacterDataFromElement(pdf));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static String getCharacterDataFromElement(Element e) {
        Node child = e.getFirstChild();
        if (child instanceof CharacterData) {
            CharacterData cd = (CharacterData) child;
            return cd.getData();
        }
        return "?";
    }

}

答案 1 :(得分:1)

SimpleXml可以做到:

final SimpleXml simple = new SimpleXml();
final Element element = simple.fromXml(data);
element.children.remove(1);
System.out.println(simple.domToXml(element));

将输出:

<getResult><id>null</id></getResult>

从Maven Central:

<dependency>
    <groupId>com.github.codemonstur</groupId>
    <artifactId>simplexml</artifactId>
    <version>1.4.0</version>
</dependency>