簡體   English   中英

寫一個XML文件問題

[英]Writing on a XML file issue

我一直在嘗試在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);
}

}

而不是adoptNode,將其設為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);
    }

adoptNode嘗試從另一個文檔中采用一個節點到此文檔。

appendChild這將節點newChild添加到此節點的子節點列表的末尾。 如果newChild已經在樹中,則首先將其刪除。

請嘗試如下;

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM