0

I need to replace the values of the sEmployee and sWUnumber (Highlighted in yellow above)

So far what i can do is replace the node value and other attributes. But when in tags. I cannot seem to replace the sEmployee and SWUnumber. I assume that these elements are not attributes?

Heres what i have done so far.

    DocumentBuilderFactory docFactory = DocumentBuilderFactory
            .newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(Constant.Path_OldXmlFile);

    // Get Employee ID, I'm getting my values in excel data so don't mind this
    String sNewEmployeeID = ExcelUtils.getCellData(iTestCaseRow,
            Constant.Personnel_NewEmployeeID);

    // Get Work Unit Number, I'm getting my values in excel data so don't mind this
    String sWorkUnitNumber = ExcelUtils.getCellData(iTestCaseRow,
            Constant.Personnel_WorkUnit);
1
  • 1
    Get the Node BODID, get it's value, use a simple String#replace and then set the new value back to the Node and save Commented Oct 20, 2015 at 3:21

1 Answer 1

1

You could use xPath to query the document for the node your after and replace it's text content, for example

try {
    // Load the document
    DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
    DocumentBuilder b = f.newDocumentBuilder();
    Document original = b.parse(...);

    // Surgically locate the node you're after
    String expression = "/SyncPersonnel/ApplicationArea/BODID";
    XPath xPath = XPathFactory.newInstance().newXPath();
    Node node = (Node) xPath.compile(expression).evaluate(original, XPathConstants.NODE);
    // Get the nodes current text content
    String value = node.getTextContent();
    System.out.println(value);

    // Replace the values
    value = value.replace("sEmployee", "BananaMan").replace("sWUnumber", "007");
    // Set the text content with the new value
    node.setTextContent(value);

    // Save the new document
    try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {

        Transformer tf = TransformerFactory.newInstance().newTransformer();
        tf.setOutputProperty(OutputKeys.INDENT, "yes");
        tf.setOutputProperty(OutputKeys.METHOD, "xml");
        tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

        DOMSource domSource = new DOMSource(original);
        StreamResult sr = new StreamResult(os);
        tf.transform(domSource, sr);

        String text = new String(os.toByteArray());
        System.out.println(text);

    } catch (TransformerException ex) {
        ex.printStackTrace();
    }

} catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException | DOMException exp) {
    exp.printStackTrace();
}

Using...

<?xml version="1.0" encoding="UTF-8"?>
<SyncPersonnel>
  <ApplicationArea>
    <BODID>...-nid:LSAPPS:3004::sEmployee:0?Personnel&amp;verb=Sync&amp;workunit=sWUnumber</BODID>
  </ApplicationArea>  
</SyncPersonnel>

the above code will produce

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SyncPersonnel>
  <ApplicationArea>
    <BODID>...-nid:LSAPPS:3004::BananaMan:0?Personnel&amp;verb=Sync&amp;workunit=007</BODID>
  </ApplicationArea>  
</SyncPersonnel>
Sign up to request clarification or add additional context in comments.

2 Comments

@user3713453 It's a relatively basic/cut down version, but should give you something to work from
Work like a charm! Thanks for this. Tagging as Answered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.