从java代码生成XML文件
例如,我想通过使用java和DOM生成以下xml文件从java代码生成XML文件,java,xml,Java,Xml,例如,我想通过使用java和DOM生成以下xml文件 <catalogue> <books> <book id="1"> <name>Gone with the wind</name> <quantity>2</quantity> </book> <book id="2">
<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>
然后我使用相同的代码创建了第二个和第三个book元素,我发现了错误。
还有别的办法吗?
谁能给我一个建议吗?
非常感谢您抽出时间如果问题是这样的,那么您已经用相同的名称声明了多个局部变量。在所有编程语言中,同一作用域中只能声明一个同名变量。作用域通常用大括号括起来。如果使用其他作用域缩进变量名,则可以在同一方法中使用相同的变量名,例如,如下面的示例所示 但是,您应该考虑变量的命名,或者是否应该使用循环语句。您还可以为变量编号,例如name1、name2、name3等 如果您真的想让多个变量具有相同的名称,您可以使用未命名的代码块将它们分开,只需使用如下大括号:
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
变量都存在于seprate作用域中,因此它们不会相互干扰,也不会导致“复制局部变量名”编译器错误消息。我猜您是在两次附加同一对象。每次都需要调用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);
你能告诉我们你从哪里得到错误的代码吗?就有效的XML而言,它看起来是正确的,所以我不知道为什么在没有看到Java代码的情况下会出现错误。我使用:Element book=doc.createElement(“book”)来创建第一个book元素,当我使用相同的代码生成另一个book节点时,它出错了。我想到了for循环,但我不知道如何处理子节点。感谢您的快速响应您是否阅读了XML以了解生成了什么DOM?您应该能够在调试器中查看DOM。@用户:请再说一遍:您能显示代码中出现错误的地方吗?这可能很简单,但如果不向我们展示,我们就帮不了你。(问题下面有一个方便的“编辑”链接…)DOMAPI是一个猪。如果你要做这样的DOM操作,我建议使用一些更为用户友好的东西,比如JDOM、XOM或DOM4JT。谢谢你,我认为它一定有用。谢谢安德鲁,看看你的代码,我发现自己真的很愚蠢。再次非常感谢
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);