XML : Why does it changes the Structure of the XML tags. JAVA

Imagine this XML:

      <?xml version="1.0" encoding="UTF-8" standalone="no"?>  <DocumentElement>      <PropsAndValues>          <Name># CONFIGURATION_NODE # DO NOT DELETE THIS ROW #</Name>          <Keywords>#</Keywords>          <Tests>#</Tests>          <Type>#</Type>      </PropsAndValues>      <PropsAndValues>          <Name>ComplexSuiteTC-1</Name>          <Keywords>ComplexSuiteTC-1</Keywords>          <Tests>Definition:"Business Process":ComplexSuiteTC-1</Tests>          <Type>Process Keyword</Type>      </PropsAndValues>  </DocumentElement>    

Tyhen I developed the code above to create , and append ofc, a "PropsAndValue" node to the "DocumentElement" node.

  public static final String xmlFilePath = PATH_TO_XML; //It does not matter here.     static final String DOC_ELEM = "DocumentElement";  static final String PROPS = "PropsAndValues";  static final String NAME = "Name";  static final String KWS = "Keywords";  static final String TESTS = "Tests";  static final String TYPE = "Type";  static final String CONFIG_NODE = "# CONFIGURATION_NODE # DO NOT DELETE THIS ROW #";  static final String SIGN = "#";  static final String DEFINITION = "Definition:\"Business Process\":";                   public void addNode(String testCaseName, String typeKeyword) throws Exception {          DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();      DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();      Document document = documentBuilder.parse(xmlFilePath);      Element root = document.getDocumentElement();                  org.w3c.dom.Node nodeToAppend = createKeyWord(document, testCaseName, typeKeyword);           root.appendChild( nodeToAppend);               DOMSource source = new DOMSource(document);      TransformerFactory transformerFactory = TransformerFactory.newInstance();      Transformer transformer = transformerFactory.newTransformer();      StreamResult result = new StreamResult(xmlFilePath);      transformer.transform(source, result);      }    private static org.w3c.dom.Node createKeyWord(Document document, String testCaseName, String typeKeyword) {         Element propsAndValuesTag = document.createElement(PROPS);         Element nameTag = document.createElement(NAME);       nameTag.appendChild(document.createTextNode(testCaseName));          propsAndValuesTag.appendChild(nameTag);         Element kwTag = document.createElement(KWS);       kwTag.appendChild(document.createTextNode(testCaseName));          propsAndValuesTag.appendChild(kwTag);         Element testsTag = document.createElement(TESTS);       testsTag.appendChild(document.createTextNode(DEFINITION + testCaseName));          propsAndValuesTag.appendChild(testsTag);         Element typeTag = document.createElement(TYPE);       typeTag.appendChild(document.createTextNode(typeKeyword));          propsAndValuesTag.appendChild(typeTag);          return  propsAndValuesTag;        }    

The code actually creates and appends the new node but it changes the Tag Structure of the file and I don't want that to happen.

New (wrong) structure:

      <?xml version="1.0" encoding="UTF-8" standalone="no"?><DocumentElement>      <PropsAndValues>          <Name># CONFIGURATION_NODE # DO NOT DELETE THIS ROW #</Name>          <Keywords>#</Keywords>          <Tests>#</Tests>          <Type>#</Type>      </PropsAndValues>      <PropsAndValues>          <Name>ComplexSuiteTC-1</Name>          <Keywords>ComplexSuiteTC-1</Keywords>          <Tests>Definition:"Business Process":ComplexSuiteTC-1</Tests>          <Type>Process Keyword</Type>      </PropsAndValues>  <PropsAndValues><Name>PaintingSuiteTC-1</Name><Keywords>PaintingSuiteTC-1</Keywords><Tests>Definition:"Business Process":PaintingSuiteTC-1</Tests><Type>Process Keyword</Type></PropsAndValues></DocumentElement>    

Can anyone tell me some way to that does not happen please? I need to remaind with the same structure after node insertion!

Thanks in advance.

No comments:

Post a Comment