java - 关于 XML 文件的写入问题

标签 java xml

我一直在尝试在 XML 文件中写入一些内容,但什么也没写入,不知道为什么。 有什么帮助吗?

这是代码:

这是我在 XML 文件上写入的方法:

public static void writeXMLFile() throws ParserConfigurationException, FileNotFoundException, IOException
{
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document xmlDoc = docBuilder.newDocument();
    /*<Drawer>
     * <Shape>
     *  <type></type>
     *  <color>
     *  <x1>
     *  <y1>
     *  <x2>
     *  <y2>
     * 
     */
    Element rootElement = xmlDoc.createElement("Drawing");
    Element mainElement= xmlDoc.createElement("Shape");
    mainElement.setAttribute("Color", "red");
    Text shapesTypeText = xmlDoc.createTextNode("Square");
    Element shapeType= xmlDoc.createElement("type");
    shapeType.appendChild(shapesTypeText);
    mainElement.appendChild(shapeType);
    rootElement.appendChild(mainElement);
    xmlDoc.adoptNode(rootElement);

    OutputFormat outFormat = new OutputFormat(xmlDoc);
    outFormat.setIndenting(true);

    File xmlFile = new File("saved.xml");

    FileOutputStream outStream = new FileOutputStream (xmlFile);

    XMLSerializer serializer = new XMLSerializer(outStream,outFormat);
    serializer.serialize(xmlDoc);
}

}

最佳答案

将其设为appendChild,而不是adoptNode

public static void main(String[] args) throws ParserConfigurationException, FileNotFoundException, IOException
    {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document xmlDoc = docBuilder.newDocument();
        /*<Drawer>
         * <Shape>
         *  <type></type>
         *  <color>
         *  <x1>
         *  <y1>
         *  <x2>
         *  <y2>
         * 
         */
        Element rootElement = xmlDoc.createElement("Drawing");
        Element mainElement= xmlDoc.createElement("Shape");
        mainElement.setAttribute("Color", "red");
        Text shapesTypeText = xmlDoc.createTextNode("Square");
        Element shapeType= xmlDoc.createElement("type");
        shapeType.appendChild(shapesTypeText);
        mainElement.appendChild(shapeType);
        rootElement.appendChild(mainElement);
        **xmlDoc.appendChild(rootElement);**

        OutputFormat outFormat = new OutputFormat(xmlDoc);
        outFormat.setIndenting(true);

        File xmlFile = new File("saved.xml");

        FileOutputStream outStream = new FileOutputStream (xmlFile);

        XMLSerializer serializer = new XMLSerializer(outStream,outFormat);
        serializer.serialize(xmlDoc);
    }

采用节点 这尝试将另一个文档中的节点采用到此文档中。

追加子项 这会将节点 newChild 添加到该节点的子节点列表的末尾。如果 newChild 已经在树中,则首先将其删除。

关于java - 关于 XML 文件的写入问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19362864/

相关文章:

java - LWJGL 3 中的抗锯齿

java - XMLStreamReader 和一个真正的流

python - 如何使用 ElementTree (python) 解析 xml 时删除无效字符

java - 为什么 JAXB 不写出 SWT Widget?

php - 使用SimpleXML/XPath排序XML?

java - XML 日期绑定(bind)到 Java 对象日期

java - ServletContext 类型的 getContextPath() 方法未定义

java - 如何防止 STRUTS 保留选择?

java - 如何从大型 XML 中获取特定元素的值

java - try/catch/finally 的惊人输出?