正如標題所示,我嘗試使用for循環將元素添加到xml文檔。 我有一個字符串ArrayList
稱爲names
,我希望遍歷,併爲每個名字創建屬性name
,並與孩子<record>
具有屬性id
,time
,date
和project
一個<user>
元素。HIERARCHY_REQUEST_ERR嘗試將元素添加到for循環中的xml文件
不幸的是,如果你在下面的代碼爲createDoc()
方法向下滾動,當我嘗試打電話doc.appendChild(user)
,我得到以下錯誤:
Exception in thread "main" org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted.
at org.apache.xerces.dom.CoreDocumentImpl.insertBefore(Unknown Source)
at org.apache.xerces.dom.NodeImpl.appendChild(Unknown Source)
at test.XMLwriter.createDoc(XMLwriter.java:131)
at test.XMLwriter.<init>(XMLwriter.java:116)
at test.TestRunner.main(TestRunner.java:33)
我已經看過那些對計算器的幾個問題同樣的錯誤,但它們似乎都發生在與我的完全不同的情況下。我最好的猜測是,這個錯誤與我嘗試在同一層次級別創建太多父元素的事實有關,但我不確定,而且我幾乎沒有使用xml的經驗。
下面是代碼:
public class XMLwriter {
private ArrayList<String> names;
private Document doc;
private Random rand;
private ArrayList<Element> users;
public XMLwriter() throws ParserConfigurationException, TransformerException{
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
doc = docBuilder.newDocument();
rand = new Random();
users = new ArrayList<Element>();
names = new ArrayList<String>();
names.add("Ralph Wiggum");names.add("Mr. Hanky");names.add("Bulbasaur");
names.add("Tyroil Smoochie Wallace");names.add("Scooby Doo");names.add("Neville Longbottom");
names.add("Jabba the Hutt");names.add("Silky Johnson");names.add("Master Chief");
names.add("Frodo Baggins");names.add("Clayton Bigsby");names.add("John Snow");
names.add("Eric Cartman");names.add("Leoz Maxwell Jilliumz");names.add("Aslan");
createDoc();
generateFile();
}
public void createDoc(){
for(int k = 0; k < names.size(); k++)
{
users.add(doc.createElement("user"));
}
for (int x = 0; x < names.size(); x++){
//create the elements
Element record = doc.createElement("record");
users.get(x).appendChild(record);
doc.appendChild(users.get(x));//The line that is throwing the error
//create the attributes
Attr name = doc.createAttribute("name");
Attr date = doc.createAttribute("date");
Attr project = doc.createAttribute("project");
Attr time = doc.createAttribute("time");
Attr id = doc.createAttribute("id");
//give all of the attributes values
name.setValue(names.get(x));
date.setValue(new Date().toString());
project.setValue("Project" + (rand.nextDouble() * 1000));
time.setValue("" + rand.nextInt(10));
id.setValue("" + (rand.nextDouble() * 10000));
//assign the attributes to the elements
users.get(x).setAttributeNode(name);
record.setAttributeNode(date);
record.setAttributeNode(project);
record.setAttributeNode(time);
record.setAttributeNode(id);
}
}
public void generateFile() throws TransformerException{
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("C:\\Users\\sweidenkopf\\workspace\\test\\testxml.xml"));
// Output to console for testing
// StreamResult result = new StreamResult(System.out);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(source, result);
}
}
我寫了這個代碼排序概念證明的,因爲我最終還是要實際執行類似的操作,在那裏我將有一個列表對象中的每一個,我必須將其添加到xml文件中,方法是使用get
方法將對象分離爲其組成部分。
任何人都可以幫我解決這個錯誤嗎?