I have an xml file:
<pickingOrderBeginEventMessage xmlns="http://www.xmlns.walmartstores.com/SuppyChain/FulfillmentManagement/GlobalIntegeratedFulfillment/">
<MessageBody>
<RoutingInfo>
<SourceNode>
<location>
<countryCode>US</countryCode>
</location>
</SourceNode>
</RoutingInfo>
<fulfillmentOrders>
<fulfillmentOrder>
<orderNbr>784</orderNbr>
</fulfillmentOrder>
</fulfillmentOrders>
</MessageBody>
</pickingOrderBeginEventMessage>
I want to change <orderNbr>784</orderNbr> to <orderNbr>784778474484747</orderNbr>
This is my method:
(Note that I am using dom4j.)
public String replaceXML(String attribute,String oldValue, String newValue) throws SAXException, DocumentException, IOException, TransformerException {
SAXReader xmlReader = new SAXReader();
Document input = xmlReader.read("src/test/resources/xml/pick_begin.xml");
xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", false);
xmlReader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
String expr = String.format("//*[contains(@%s, '%s')]", attribute, oldValue);
XPath xpath = DocumentHelper.createXPath(expr);
List<Node> nodes = xpath.selectNodes(input);
for (int i = 0; i < nodes.size(); i++) {
Element element = (Element) nodes.get(i);
element.addAttribute(attribute, newValue);
}
TransformerFactory factory = TransformerFactory.newInstance();
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
Transformer xformer = factory.newTransformer();
xformer.setOutputProperty(OutputKeys.INDENT, "yes");
Writer output = new StringWriter();
xformer.transform(new DocumentSource(input), new StreamResult(output));
return output.toString();
}
}
Where String attribute is orderNbr, oldValue is 784 and newValue is 78455556767.
But with this method, the new value is not getting replaced. Where am I going wrong?
According to the XML file in your question,
orderNbris an element and not an attribute and its text value is 784. So you want to replace the text value with 78455556767.Your code does not change the original XML because your XPath query string does not find anything.
Therefore you need to change two things in your code.
The below code contains the two changes. The changed lines are indicated with the following comment at the end of the line.