how to add new node under a specific node in dom4j JAVA

774 views Asked by At

I want to generate flexiField tags and add it to current xml file.

Here is My 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>

I want to add new flexiField and I have this Java Code but When I run my code, it adds flexiField node under <com.model.FlexiFieldList> element

I know that in Element root = document.getDocumentElement(); part we show that we will add new node to root location but I couldnt find any way to change it

Actually My question is that how can I change root path so when I run root.appendChild(newFlexiField); it will add new flexiField node under flexiFields node

    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

There are 1 answers

0
Enjy On

Assumming you have only one <flexiFields> element in you xml file you can try this:

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);