从java代码生成XML文件

发布于 2024-10-10 05:39:47 字数 1008 浏览 0 评论 0原文

例如,我想使用java和DOM生成以下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 元素,然后发现错误。 还有其他方法吗? 有人可以给我一个建议吗? 非常感谢您抽出时间

For example,I want to generate the following xml file by using java with DOM

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

It's not very difficult to produce xml file with only 1 node named book, but with more than 1 with the same name, I dont know how to do it? I got the error:

Duplicate local variable

This is one part of my java code:
I tried to create the first book element with the code:

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

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

And then I used the same code to create the second and the third book element, and I found the error.
Is there any other way to do it?
Can anyone give me a suggestion please?
Thank you very much for your time

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒岛晴空 2024-10-17 05:39:47

我猜您将同一个对象附加两次。每次都需要调用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);

I'm guessing you are appending the same object twice. You need to call createElement each time.

This won't work

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

You need to do

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

Complete example

     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);
美胚控场 2024-10-17 05:39:47

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

但是,您应该考虑变量的命名,或者是否应该使用循环语句。您还可以对变量进行编号,例如 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 变量都位于单独的作用域中,因此它们不会相互干扰,也不会导致“重复的局部变量名称”编译器错误消息。

If the problem is this, then you've declared multiple local variables with the same name. In all programmling languages, you can only have one variable with the same name declared in the same scope. A scope is usually enclosed by curly braces. You can use the same variable name in the same method if you indent them with additional scopes, e.g. like the example below.

However, you should think about the naming of your variables, or if you should instead make use of loop statements. You could also number your variables, e.g. name1, name2, name3 etc.

alt text

If you really want to have multiple variables with the same name, you can separate them by unnamed code blocks by just using the curly braces like this:

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

Both name variables life in seprate scopes, so they don't interfere with each other and will not lead to the "Duplicate local variable name" compiler error message.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文