如何在html中修改手动添加的标记值

时间:2016-06-22 09:51:39

标签: java html apache-poi

        class HtmlTagmodifier {    
        public String htmlFileWriter(String cfile, String Listname, String Nodename, String nodevalue) {
        try {
        File fhtmlFile = new File(cfile);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fhtmlFile);
        doc.getDocumentElement().normalize();

        NodeList nList = doc.getElementsByTagName(Listname);
        for (int temp = 0; temp < nList.getLength(); temp++) {
            Node nNode = nList.item(temp);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;
                eElement.getElementsByTagName(Nodename).item(0).setTextContent(nodevalue);
            }
        }
        Source source = new DOMSource(doc);
        Result htmlresult = new StreamResult(fhtmlFile);
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.transform(source, htmlresult);
        result2 = "Success";

       } catch (Exception e) {
        e.printStackTrace();
        log.error("Error in html file writing " + e.toString());
        JOptionPane.showMessageDialog(null, "Error in html file writing " + e.toString());
        result2 = "Failed";
        }
        return result2;     
        }

        public static void main(String[] args) {
        HtmlTagmodifier.htmlfilewriter("test.html", "details", "customername", "customernamexxxxxx");
        }
         }

输出: enter image description here 当我使用此方法修改html的标记值时,标记名称已成功更改,但元数据标记在html中再次添加

请给我一些建议。

0 个答案:

没有答案