如何在JDOM JAVA中的特定节点下添加新节点

时间:2017-01-08 19:01:23

标签: java xml dom4j

我想生成flexiField标记并将其添加到当前的xml文件中。

这是我的xmlFile。

<com.model.FlexiFieldList>
    <flexiFields>
      <flexiField>
        <fieldDesc>BL</fieldDesc>
        <fieldName>BL</fieldName>
        <fieldType>FREE_TEXT</fieldType>
        <fieldTypeDataStr/>
        <sensitive>false</sensitive>
        <value>True</value>
        <valueType>String</valueType>
      </flexiField>
    </flexiFields>
</com.model.FlexiFieldList>

我想添加新的flexiField,我有这个Java代码但是当我运行我的代码时,它在<com.model.FlexiFieldList>元素下添加了flexiField节点

我知道在Element root = document.getDocumentElement();部分中我们将展示我们将向根位置添加新节点但我无法找到任何方法来更改它

实际上我的问题是如何更改根路径以便在运行root.appendChild(newFlexiField);时它会在flexiField节点下添加新的flexiFields节点

    File file = new File("C:/Users/xxx/Desktop/ff.xml");
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(file);
    Element root = document.getDocumentElement();
    Collection<FlexiField> flexiFields = new ArrayList<FlexiField>();
    flexiFields.add(new FlexiField());

    for (FlexiField flexiField : flexiFields) {
        // server elements
        Element newFlexiField = document.createElement("flexiField");

        Element name = document.createElement("fieldDesc");
        name.appendChild(document.createTextNode(flexiField.fieldDesc()));
        newFlexiField.appendChild(name);

        Element port = document.createElement("fieldName");
        port.appendChild(document.createTextNode(flexiField.fieldName()));
        newFlexiField.appendChild(port);


        root.appendChild(newFlexiField);

    }

    DOMSource source = new DOMSource(document);

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    StreamResult result = new StreamResult(file);
    transformer.transform(source, result);

1 个答案:

答案 0 :(得分:0)

假设您的xml文件中只有一个<flexiFields>元素,您可以尝试这样做:

File file = new File("C:/Users/xxx/Desktop/ff.xml");
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(file);
    Element root = document.getDocumentElement();

    // to print your root element
    System.out.println("\n*************root************");
    System.out.println(root.getNodeName());

    //  to find the  flexiFields
    Node firstChild=root.getFirstChild();

    while (firstChild != null && firstChild.getNodeType() != Node.ELEMENT_NODE)
    {
        firstChild = firstChild.getNextSibling();
    }

    System.out.println(firstChild.getNodeName());


   Collection<FlexiField> flexiFields = new ArrayList<FlexiField>();
    flexiFields.add(new FlexiField());

    for (FlexiField flexiField : flexiFields) {
        // server elements
        Element newFlexiField = document.createElement("flexiField");

        Element name = document.createElement("fieldDesc");
        name.appendChild(document.createTextNode(flexiField.fieldDesc()));
        newFlexiField.appendChild(name);

        Element port = document.createElement("fieldName");
        port.appendChild(document.createTextNode(flexiField.fieldName()));

        // append your child
        newFlexiField.appendChild(port);


        firstChild.appendChild(newFlexiField);

    }

    DOMSource source = new DOMSource(document);

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    StreamResult result = new StreamResult(file);
    transformer.transform(source, result);