栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

尝试在for循环中将元素添加到xml文件时出现HIERARCHY_REQUEST_ERR

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

尝试在for循环中将元素添加到xml文件时出现HIERARCHY_REQUEST_ERR

我发现了这个问题的答案。方法如下:

我创建了另一个层次结构层,

<userList>
每次迭代for循环时,我都会使新创建
<user>
的子层为
<userList>

最后,超出了for循环的范围,我

<userList>
将xml文档作为整个子文档。

这是那些有兴趣的新代码。您可以阅读

createDoc()
方法中的注释,以帮助弄清楚我在上面解释的内容:

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(){    Element userList = doc.createElement("userList");//here, I create a new, over-arching element.    for(int k = 0; k < names.size(); k++)    {        users.add(doc.createElement("user"));    }    for (int x = 0; x < 2; x++){        //create the elements        Element record = doc.createElement("record");        users.get(x).appendChild(record);        userList.appendChild(users.get(x));//I make each of the <user> elements a child of the over-arching element        //The line that was 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);    }    doc.appendChild(userList);//note how I append this child outside of the for loop}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);}}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/446881.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号