0

我想修改一个 html(XML) 来添加一个节点。但在那之后,也添加了一个元素。该行将使文件损坏。太奇怪了。任何人都可以看看吗?

这是代码。

private void linkPrepare(String buildNum) {
    // TODO Auto-generated method stub
    DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
    DocumentBuilder dombuilder;
    try {
        dombuilder = domfac.newDocumentBuilder();
        InputStream is = new FileInputStream(
                "File/test");
        Document doc = dombuilder.parse(is);

        NodeList nodes = doc.getElementsByTagName("div");
        Node firstNode = nodes.item(0);
        Element newNode = doc.createElement("ul");

        firstNode.appendChild(newNode);

        TransformerFactory transformerFactory = TransformerFactory
                .newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(
                "File/test"));
        transformer.transform(source, result);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

这是html文件,

<html>
<head>
<title>OTA</title>
</head>
<body>
<div>
<ul></ul>
</div>
</body>
</html>
4

0 回答 0