Java中如何保存DOM文档?

发布于 2024-10-22 23:08:20 字数 1532 浏览 4 评论 0原文

我正在使用 DOM 解析器和 XPATH 来解析我的 XML 文件。我更改了文档对象中的节点值。但是,当我打开 XML 文件时,它没有显示任何反射。我的 DOM 解析器代码如下:

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;
    }

完成更改后如何保存文档?

谁能帮我解决这个问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苍景流年 2024-10-29 23:08:20

以下是更新 XML 文件的示例代码

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();
}

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();
}
苏别ゝ 2024-10-29 23:08:20

您可以使用 Transformer API 将 DOM 写入流或文件。

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

霞映澄塘 2024-10-29 23:08:20

您必须使用转换器将 dom 对象转换为 XML。

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文