PHP код:
public static void main(String[] args) throws IOException, ParserConfigurationException, TransformerException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document document;
try {
document = factory.newDocumentBuilder().parse(new File("test.xml"));
final NodeList nodeList = document.getFirstChild().getChildNodes();
IntStream.range(0, nodeList.getLength()).forEach(i -> {
Node node = nodeList.item(i);
IntStream.range(0, node.getChildNodes().getLength()).forEach(a -> {
final Node nodeBlock = node.getChildNodes().item(a);
if(nodeBlock.hasAttributes()) {
IntStream.range(0, nodeBlock.getAttributes().getLength()).forEach(indexAtt -> {
Node attribute = nodeBlock.getAttributes().item(indexAtt);
if(attribute.getNodeName().equals("name")) {
attribute.setNodeValue("finfan " + ThreadLocalRandom.current().nextInt());
}
else {
attribute.setNodeValue(String.valueOf(ThreadLocalRandom.current().nextInt()));
}
});
}
});
});
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(document), new StreamResult(new File("new_test.xml")));
}
catch(SAXException e) {
e.printStackTrace();
}
}
old xml ...
PHP код:
<list>
<test name="finfan">
<set name="finfan" value="replace"/>
</test>
<test name="finfan">
<set name="finfan" value="replace"/>
</test>
<test name="finfan">
<set name="finfan" value="replace"/>
</test>
</list>
new replace all xml =\
PHP код:
<?xml version="1.0" encoding="UTF-8" standalone="no"?/>
<list>
<test name="finfan">
<set name="finfan -1501716197" value="-977792414"/>
</test>
<test name="finfan">
<set name="finfan -392286072" value="-230381045"/>
</test>
<test name="finfan">
<set name="finfan 1510237655" value="1910193530"/>
</test>
</list>