简体   繁体   English

从Java代码生成XML文件

[英]Generate XML file from java code

For example,I want to generate the following xml file by using java with DOM 例如,我想通过使用带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>

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? 生成只有1个名为book的节点的xml文件并不是很难,但是如果名称相同的多于1个,我不知道怎么做? 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: 这是我的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);

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 非常感谢您的宝贵时间

I'm guessing you are appending the same object twice. 我猜你要两次追加同一个物体。 You need to call createElement each time. 您需要每次都调用createElement。

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

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, eg 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, eg name1, name2, name3 etc. 您也可以为变量编号,例如name1,name2,name3等。

替代文字

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. 这两个name变量都存在于单独的范围内,因此它们不会互相干扰,也不会导致出现“重复的局部变量名称”编译器错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM