How do you save DOM Document in Java?

2019-07-20 16:57发布

I am using DOM parser and XPATH to parse a my XML file. I have changed a value of a node in the Document Object. However when I open my XML file, it doesn't show me any reflection. My DOM parser code is as below :

private void setPortNumber(int portNumber) {
        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(parsedDocument, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node =nodes.item(i);
            NamedNodeMap attributes = node.getAttributes();
            for(int j=0; j< attributes.getLength(); j++){
                String value = attributes.item(j).getNodeValue();
                if(value.equals("HTTP/1.1")){
                    Node valueNode = attributes.item(0);
                    valueNode.setNodeValue(portNumber+"");
                }
            }
        }
        } catch (XPathExpressionException e) {}
    }
    private Document parseDocument(String xmPath) {
        Document doc = null;
        try {
            DocumentBuilderFactory domFactory = 
                DocumentBuilderFactory.newInstance();
            DocumentBuilder builder;

            builder = domFactory.newDocumentBuilder();

            doc = builder.parse(xmPath);
        }catch (Exception e) {}
        return doc;
    }

How can I save my document after done with changes?

Can anyone help me to resolve this?

3条回答
beautiful°
2楼-- · 2019-07-20 17:26

You have to transform the dom object into an XML using the transformer.

http://download.oracle.com/javaee/1.4/tutorial/doc/JAXPXSLT4.html

查看更多
狗以群分
3楼-- · 2019-07-20 17:43

Here is the sample code for updating an XML file

try
{
  DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
  DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
  Document doc = docBuilder.parse(filePath);
  Node rootNode = doc.getFirstChild();//for getting the root node

  String expersion="books/author";//x-path experssion

  XPathFactory factory = XPathFactory.newInstance();
  XPath xpath = factory.newXPath();
  XPathExpression expr = xpath.compile(expersion);
  Node updateNode=null;
  Object result = expr.evaluate(doc, XPathConstants.NODESET);
  NodeList nodes = (NodeList) result;
  updateNode=nodes.item(0);
  updateNode.appendChild(doc.createCDATASection("new value"));
  TransformerFactory transformerFactory = TransformerFactory.newInstance();
  Transformer transformer = transformerFactory.newTransformer();
  DOMSource source = new DOMSource(doc);
  StreamResult streamResult =  new StreamResult(new File(filePath));
  transformer.transform(source, streamResult);
}
catch (Exception e) {
  e.printStackTrace();
}
查看更多
混吃等死
4楼-- · 2019-07-20 17:45

You could use the Transformer API to write your DOM to a stream or a file.

查看更多
登录 后发表回答