0

XMLファイルを作成して自分のコンピューターにローカルに保存したい次のコードがありますが、コードを実行するたびにファイルが保存されていないようです。誰か助けてください。

public void CreateXMLPoll (Poll p) {

        try {
            // Initialize the XML builder
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder;
            docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.newDocument();

            // Set the Elements Up
            Element rootElement = doc.createElement("Poll");
            Element creator = doc.createElement("Creator");
            Element name = doc.createElement("Name");
            Element email = doc.createElement("Email");
            Element title = doc.createElement("Title");
            Element location = doc.createElement("Location");
            Element description = doc.createElement("Description");
            Element date = doc.createElement("Date");
            Element time = doc.createElement("Time");

            // Create XML tree
            rootElement.appendChild(creator);
            rootElement.appendChild(title);
            rootElement.appendChild(location);
            rootElement.appendChild(description);
            rootElement.appendChild(date);
            creator.appendChild(name);
            creator.appendChild(email);
            date.appendChild(time);

            // Add values to XML
            name.appendChild(doc.createTextNode(p.getUsername()));
            email.appendChild(doc.createTextNode(p.getEmail()));
            title.appendChild(doc.createTextNode(p.getTitle()));
            location.appendChild(doc.createTextNode(p.getLocation()));
            description.appendChild(doc.createTextNode(p.getDescription()));

            // Appends a bunch of dates
            for (PollDates pd : p.getDates()) {
                time.appendChild(doc.createTextNode(pd.getFirst().toString()));
                if (pd.getSecond() != null) time.appendChild(doc.createTextNode(pd.getSecond().toString()));
                if (pd.getThird() != null) time.appendChild(doc.createTextNode(pd.getThird().toString()));
            }

            // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            try {
                Transformer transformer = transformerFactory.newTransformer();
            } catch (TransformerConfigurationException e) {
                e.printStackTrace();
            }
            FileWriter fstream = new FileWriter("test.xml");
            BufferedWriter out = new BufferedWriter(fstream);

            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new File("C:/Users/testing.xml"));

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

ここでチュートリアルに従いました。

4

2 に答える 2

3

この行の後、

BufferedWriter out = new BufferedWriter(fstream);

次のようなステートメントが必要です。

out.write("content to write in the file");

ファイルを書き込みます。

コンテンツを取得するために、チュートリアルからこれを見逃しました:

StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
trans.transform(source, result);
String xmlString = sw.toString();

を取得したら、次xmlStringを使用してファイルに書き込みます。

out.write(xmlString,0, xmlString.length);

完了したら、追加する必要があります。

out.close();

ストリームを閉じます。

プログラムを更新して試してください。

于 2012-10-12T05:13:39.553 に答える
2

JAXB を使用することをお勧めします

JAXB を使用すると、次のことが可能になります。

  • XML スキーマから JAXB Java クラスを生成する
  • スキーマから派生した JAXB クラスを使用して、Java アプリケーションで XML コンテンツをアンマーシャリングおよびマーシャリングする
  • スキーマから派生した JAXB クラスを使用して Java コンテンツ ツリーを作成する
  • 非整列化中および実行時に XML コンテンツを検証する
  • JAXB スキーマと Java のバインディングをカスタマイズする

この例を確認してください

http://www.vogella.com/articles/JAXB/article.html

http://www.mkyong.com/java/jaxb-hello-world-example/

より簡単で、読みやすいコード、オブジェクト指向のアプローチです。

于 2012-10-12T06:00:30.767 に答える