xml - How do you save DOM Document in Java? -


I am using DOM parser and XPATH my pars To XML file. I changed the value of a node in the document object , even though when I open my XML file, it does not show me any reflection on my code The parser code is given below:

  Private zeros set port number (int port number) {try {document parsedDocument = parseDocument (temppath + "/ apache-6 / conf / server.xml") ; XPath xPath = XPathFactory.newInstance (). NewXPath (); XPathExpression expr; Expr = (XPathExpression) xPath.compile ("// connector"); Object result = expr.evaluate (Persecuted Documents, XPathConstants.NODESET); Nodelist nodes = (nodelist) result; For (int i = 0; i & lt; nodes.getLength (); i ++) {node node = node. Item (i); Named attribute = node.get properties (); (Int j = 0; j & lt; attributes.getLength (); j ++) {string value = attributes .item (j) .getNodeValue (); If (value.equals ("HTTP / 1.1") {node valueNode = attributes.item (0); ValueNode.setNodeValue (portnumber + ""); }}}} Hold (XPathExpressionException e) {}} private document parseDocument (string xmPath) {document document = null; Try {DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance (); Document Builder Builder; Builder = domFactory.newDocumentBuilder (); Doctor = Builder. Pear (Xmapath); } Hold (exception e) {} return doctor; }   

How can I save my document after the changes?

Can anyone help me solve this?

The sample code for updating an XML file is

  Try {documentbuilderfactory docFactory = DocumentBuilderFactory.newInstance (); DocumentBuilder Debuilder = docFactory.newDocumentBuilder (); Document doctor = docbuilder.parse (filePath); Route node string expression = "node routerode = doc." To get "books / authors"; // X-Path Expression XPathFactory Factory = XPathFactory.newInstance (); XPath xpath = Factory Nxpath (); XPathExpression expr = xpath.compile (expersion); Node updateNode = null; Object result = expr.evaluate (Doctor, XPathConstants.NODESET); Nodelist nodes = (nodelist) result; UpdateNode = nodes.item (0); UpdateNode.appendChild (doc.createCDATASection ("new value")); Transformerformer transformerfactor = transformerfactor.New instance (); Transformer transformer = Transformerfacture. NAV transformer (); DOMSource Source = New DOMSource (Doctor); StreamResult streamResult = new stream result (new file (filePath)); Transformer. Transform (source, stream resolve); } Hold (exception e) {e.printStackTrace (); }    

Comments