简体   繁体   中英

Add XML node to existing XML using JAVA code

I have an existing XML ( JMeter JMX file). What is the best way / API's to implement the following use case in Java?

My goal is to insert the following XML (create new file):

<BackendListener guiclass="A" testclass="B" testname="C" enabled="true">
        <elementProp name="D" elementType="E" guiclass="F" testclass="G" enabled="true">
          <collectionProp name="H"/>
        </elementProp>
        <stringProp name="classname">I</stringProp>
</BackendListener>
<hashTree/>

Into the following existing XML file, where the work HERE is (under jmeterTestPlan-> hashTree -> hashTree -> last node)

<jmeterTestPlan version="A">
  <hashTree>
    <TestPlan guiclass="A" testclass="B" testname="C" enabled="true">

    </TestPlan>
    <hashTree>
      <ThreadGroup guiclass="D" testclass="E" testname="F" enabled="true">

      </ThreadGroup>
      <hashTree>
        <HTTPSamplerProxy guiclass="G" testclass="H" testname="I" enabled="true">
        </HTTPSamplerProxy>
        <hashTree/>
      </hashTree>

        ****HERE****

    </hashTree>
  </hashTree>
</jmeterTestPlan>

Any suggestion will be very appreciated!

Or the ugly, simplistic approach:

Read the file as a String:

File xmlFile = new File("jmeter.xml");
String xml;
try (Scanner scanner = new Scanner(xmlFile)) {
   xml= scanner.useDelimiter("\\A").next();
} 

Find the position:

int index = xml.indexOf("</HTTPSamplerProxy>");
index = xml.indexOf("</hashTree>", index);
index += "</hashTree>".length();

Insert the new part:

String newPart = "<BackendListener guiclass=\"A\" testclass=\"B\" testname=\"C\" enabled=\"true\">
        <elementProp name=\"D\" elementType=\"E\" guiclass=\"F\" testclass=\"G\" enabled=\"true\">
          <collectionProp name=\"H\"/>
        </elementProp>
        <stringProp name=\"classname\">I</stringProp>
</BackendListener>
<hashTree/>";
String newXML = xml.substring(0, index) + newPart + xml.substring(index)

Write the new XML String back to the file:

try (PrintWriter pw = new PrintWriter(xmlFile)) {
    pw.println(newXML);
}

There are lot of libraries in java which you can use to do manipulation on XML. Do have a look on the following:

You can use this parser if you want to parse large XML files and/or don't want to use a lot of memory.

http://download.oracle.com/javase/6/docs/api/javax/xml/parsers/SAXParserFactory.html

Example: http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/

DOMParser

You can use this parser if you need to do XPath queries or need to have the complete DOM available.

http://download.oracle.com/javase/6/docs/api/javax/xml/parsers/DocumentBuilderFactory.html

Example: http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/

Using this you can do marshaling of the XML get the instance add the new instance to the end and later unmarshaling to get the xml back

If you have an XSD file for this XML then you can easily generate Java classes from that using JAXB (for example with a Maven JAXB plugin) and unmarshal your XML file into a tree of object instances. Then you could programmatically add the instance you want and marshal the tree back into XML.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM