简体   繁体   English

写一个XML文件问题

[英]Writing on a XML file issue

I have been trying to write something in a XML File, but nothing was written, don't know why. 我一直在尝试在XML文件中写一些东西,但没有写任何东西,不知道为什么。 Any help? 有帮助吗?

This is the code: 这是代码:

Here is the method I use to write on a XML File: 这是我用来在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);
}

} }

Instead of adoptNode, make it as appendChild 而不是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 This attempts to adopt a node from another document to this document. adoptNode尝试从另一个文档中采用一个节点到此文档。

appendChild This adds the node newChild to the end of the list of children of this node. appendChild这将节点newChild添加到此节点的子节点列表的末尾。 If the newChild is already in the tree, it is first removed. 如果newChild已经在树中,则首先将其删除。

Please try as follows; 请尝试如下;

            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