1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| // Création d'un document
DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
Document document = documentBuilder.parse(configFilePath);
// XPath creation
XPathFactory fact = XPathFactory.newInstance();
XPath xpath = fact.newXPath();
//XPath evaluation
XPathExpression exp = xpath.compile("/configuration/logger[@name='Lua']/level/@value='DEBUG'");
Object node = exp.evaluate(document);
// Modification du node
// ...
// Enregistrement
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(document);
File out = new File(configFilePath);
StreamResult result = new StreamResult(out);
transformer.transform(source, result); |
Partager