使用DOMParser向xml添加节点的问题?

时间:2017-06-13 10:00:34

标签: java xml parsexml

XML:

<types>
    <members>*</members>
    <name>ApexClass</name>
</types>
<version>38.0</version>

我正在尝试将xml修改为:

<types>
    <members>test1</members>
    <members>test1</members>
    <members>test1</members>
    <members>test1</members>
    <name>ApexClass</name>
</types>
<version>38.0</version>

我目前的代码如下:

public class Main {

static List<String> stringList = new ArrayList<String>();

public static void main(String[] args) {

    try {
        stringList.add("test1");
        stringList.add("test2");
        stringList.add("test3");
        File fXmlFile = new File("C:\\Jenkins\\salesforce_ant_38.0\\sample\\mypkg\\package.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        //optional, but recommended
        //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
        doc.getDocumentElement().normalize();
        NodeList nList = doc.getElementsByTagName("types");
        for (int temp = 0; temp < nList.getLength(); temp++) {
            Node parentNode = nList.item(temp);
            removeNodes(parentNode, doc);
            createChildNodes(doc, parentNode, stringList);


        }

        // write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(fXmlFile.getPath()));
        transformer.transform(source, result);

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

private static void removeNodes(Node parentNode, Document document) {
    NodeList membersList = document.getElementsByTagName("members");
    NodeList nameList = document.getElementsByTagName("name");

    for (int i = 0; i <= membersList.getLength(); i++) {
        Node memberNode = membersList.item(i);
        if(memberNode != null) {
            parentNode.removeChild(memberNode);
        }

    }

    for (int i = 0; i <= nameList.getLength(); i++) {
        Node nameNode =  nameList.item(i);
        if(nameNode != null) {
            parentNode.removeChild(nameNode);
        }
    }

}

private static void createChildNodes(Document document, Node parentNode, List<String> stringList) {
    Element version = null;
    for (int i = 0; i < stringList.size(); i++) {
        version = document.createElement("members");
        version.setTextContent(stringList.get(i));
        parentNode.appendChild(version);

    }
    version = document.createElement("name");
    version.setTextContent("ApexClass");
    parentNode.appendChild(version);



}

}

如果xml如下所示,则输出不会删除所有“成员”节点:

<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
</types>
<version>38.0</version>
</Package> 

输出将是:

<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>test1</members>
<members>test2</members>
<members>test3</members>
<name>ApexClass</name>
</types>
<version>38.0</version>
</Package>

这是正确的,但如果xml如上所述,则输出结果为:

 <Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>

<members>test2</members>


<members>test1</members>
<members>test2</members>
<members>test3</members>
<name>ApexClass</name>
</types>
<version>38.0</version>
</Package>

哪个错误,它应该删除所有成员节点,然后添加新成员和名称节点。

此外,它还在不断增加空间。

0 个答案:

没有答案