从java代码生成XML文件

时间:2011-01-04 22:35:44

标签: java xml

例如,我想使用带DOM的java

生成以下xml文件
 <catalogue>
    <books>
        <book id="1">
           <name>Gone with the wind</name>
            <quantity>2</quantity>
        </book>
        <book id="2">
           <name>Call of the wind</name>
           <quantity>3</quantity>
         </book>
         <book id="3">
           <quality>Good</quality>
          </book>
    </books>
    </catalogue>

生成只有1个名为book的节点的xml文件并不是很难,但是如果名称相同的多于1个,我不知道怎么做?我收到了错误:

重复的本地变量

这是我的java代码的一部分: 我尝试使用代码

创建第一个 book 元素
  Element book = doc.createElement("book");
  rootElement.appendChild(book);

  Element name = doc.createElement("name");
  name.appendChild(doc.createTextNode("Gone with the wind"));
  book.appendChild(name);

然后我使用相同的代码创建第二个和第三个 book 元素,我发现了错误。 还有其他办法吗? 有人可以给我一个建议吗? 非常感谢你的时间

2 个答案:

答案 0 :(得分:2)

我猜你要两次追加同一个物体。您每次都需要调用createElement。

这不起作用

    Element name = doc.createElement("name");
    name.appendChild(doc.createTextNode("Gone with the wind"));
    book.appendChild(name);

    name.appendChild(doc.createTextNode("Call of the wind"));
    book.appendChild(name);

你需要做

    Element name = doc.createElement("name");
    name.appendChild(doc.createTextNode("Gone with the wind"));
    book.appendChild(name);

    name = doc.createElement("name");
    name.appendChild(doc.createTextNode("Call of the wind"));
    book.appendChild(name);

完整示例

     Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();

    Element root = doc.createElement("catalogue");
    doc.appendChild(root);

    Element books = doc.createElement("books");
    root.appendChild(books);

    Element book1 = doc.createElement("book");
    book1.setAttribute("id", "1");
    books.appendChild(book1);

    Element book1_name = doc.createElement("name");
    book1_name.setTextContent("Gone with the wind");
    book1.appendChild(book1_name);

    Element book1_quantity = doc.createElement("quantity");
    book1_quantity.setTextContent("2");
    book1.appendChild(book1_quantity);

    Element book2 = doc.createElement("book");
    book2.setAttribute("id", "2");
    books.appendChild(book2);

    Element book2_name = doc.createElement("name");
    book2_name.setTextContent("Call of the wind");
    book2.appendChild(book2_name);

    Element book2_quantity = doc.createElement("quantity");
    book2_quantity.setTextContent("3");
    book2.appendChild(book2_quantity);

    Element book3 = doc.createElement("book");
    book3.setAttribute("id", "3");
    books.appendChild(book3);

    Element book3_quality = doc.createElement("quality");
    book3_quality.setTextContent("Good");
    book3.appendChild(book3_quality);

答案 1 :(得分:0)

如果问题是这样,那么你已经声明了多个具有相同名称的局部变量。在所有编程语言中,只能在同一范围中声明一个具有相同名称的变量。范围通常用花括号括起来。如果使用其他范围缩进它们,则可以在同一方法中使用相同的变量名称,例如:比如下面的例子。

但是,您应该考虑变量的命名,或者是否应该使用循环语句。您也可以为变量编号,例如name1,name2,name3等。

alt text

如果你真的想拥有多个具有相同名称的变量,你可以通过使用这样的大括号将它们与未命名的代码块分开:

Element book = doc.createElement("book");
rootElement.appendChild(book);

{
  Element name = doc.createElement("name");
  name.appendChild(doc.createTextNode("Gone with the wind"));
  book.appendChild(name);
}
{
  Element name = doc.createElement("name");
  name.appendChild(doc.createTextNode("Call of the wind"));
  book.appendChild(name);
}
...

两个name变量都存在于单独的作用域中,因此它们不会相互干扰,也不会导致“重复的本地变量名称”编译器错误消息。

相关问题