从java代码生成XML文件
例如,我想使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜您将同一个对象附加两次。每次都需要调用createElement。
这行不通,
您需要执行
完整示例
I'm guessing you are appending the same object twice. You need to call createElement each time.
This won't work
You need to do
Complete example
如果问题是这样的,那么您已经声明了多个具有相同名称的局部变量。在所有编程语言中,同一范围中只能声明一个同名变量。范围通常用大括号括起来。如果使用附加范围缩进,则可以在同一方法中使用相同的变量名称,例如下面的示例。
但是,您应该考虑变量的命名,或者是否应该使用循环语句。您还可以对变量进行编号,例如 name1、name2、name3 等。
如果您确实想要使用多个变量如果名称相同,您可以通过使用大括号将它们通过未命名的代码块分隔开,如下所示:
两个
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.
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:
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.