0

我一直在尝试在 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);
}

}

4

2 回答 2

2

代替采用节点,将其设为 appendChild

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

采用节点 这试图从另一个文档采用一个节点到这个文档。

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

于 2013-10-14T15:02:11.860 回答
0

请尝试如下;

            xmlDoc.appendChild(rootElement);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(xmlDoc);
    StreamResult result = new StreamResult(new File("saved.xml"));

    transformer.transform(source, result);
于 2013-10-14T15:05:44.180 回答