-1

Imagine this XML:

<DocumentElement>
  <PropsAndValues>
    <Name># CONFIGURATION_NODE # DO NOT DELETE THIS ROW #</Name>
    <Keywords>#</Keywords>
    <Tests>#</Tests>
    <Type>#</Type>
  </PropsAndValues>
  <PropsAndValues>
    <Name>PersonSuiteTC-1</Name>
    <Keywords/>
    <Tests>Definition:"Business Process":PersonSuiteTC-1</Tests>
    <Type>HIGH</Type>
  </PropsAndValues>
  <PropsAndValues>
    <Name>No Operation</Name>
    <Keywords/>
    <Tests/>
    <Type>TECH</Type>
  </PropsAndValues>
  <PropsAndValues>
    <Name>Start</Name>
    <Keywords>No Operation</Keywords>
    <Tests/>
    <Type>LOW</Type>
  </PropsAndValues>
  <PropsAndValues>
    <Name>Open Application</Name>
    <Keywords>No Operation</Keywords>
    <Tests/>
    <Type>LOW</Type>
  </PropsAndValues>
  <PropsAndValues>
    <Name>Go To Profile Finder</Name>
    <Keywords>No Operation</Keywords>
    <Tests/>
    <Type>LOW</Type>
  </PropsAndValues>
  <PropsAndValues>
    <Name>Search Person</Name>
    <Keywords>No Operation</Keywords>
    <Tests/>
    <Type>LOW</Type>
  </PropsAndValues>
  <PropsAndValues>
    <Name>End</Name>
    <Keywords>No Operation</Keywords>
    <Tests/>
    <Type>LOW</Type>
  </PropsAndValues>
  <PropsAndValues>
    <Name>PersonSuiteTC-1</Name>
    <Keywords>
    Start Open Application Go To Profile Finder Search Person End
    </Keywords>
    <Tests/>
    <Type>HIGH</Type>
  </PropsAndValues>
</DocumentElement>

I need to create a method with this header:

private static void addRelation(String kWName, String elemName) throws Exception {

}

that adds elemName to the tag <Keywords> of a Node that has <Name> tag value the kwName.

For example:

addRelation("PersonSuiteTC-1", "Add this String to <Keywords> tag"), should go to the node with the name "PersonSuiteTC-1" and add "Add this String to tag" to the <Keywords> tag.

What's the easiest way to do it?

Thanks.

Zinid
  • 61
  • 7
  • Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it. – JFPicard Jan 18 '16 at 15:25

1 Answers1

1

1 parse your XML .

Use that for example:

String xml= your xml
DocumentBuilderFactory builderFactory =DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(xml)));

2 find your point of insertion.

Use XPath, with something like that:

XPath xpath = XPathFactory.newInstance().newXPath();
expression="//Name[text()='PersonSuiteTC-1']";

XPathExpression expr = xpath.compile(expression) ; 
NodeList nodes  = (NodeList) expr.evaluate(document, XPathConstants.NODESET);

note that you have several nodes with this text

you can iterate:

for (int k = 0; k < nodes.getLength(); k++) {

  Node nodeSegment = nodes.item(k);

3 insert your data:

use createElement(), createTextNode(), appendChild()

see that: How do I append a node to an existing XML file in java

4 generate you xml back:

Transformer transformer = TransformerFactory.newInstance().newTransformer();
Result out = new StreamResult(new File("result.xml"));
Source in = new DOMSource(document);
transformer.transform(in, out); 

Hope it helps !

Community
  • 1
  • 1