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