【问题标题】:Problems with Java XML RemoveChildJava XML RemoveChild 的问题
【发布时间】:2011-08-28 01:30:50
【问题描述】:

我有以下代码:

try {
    Builder builder = new Builder();
    Document doc = builder.build(Config.PATH +"incasation.xml");

    Element root = doc.getRootElement();    
    Elements childs = root.getChildElements("locations");

    int nodeToDelete = 0;
    for(int i=0; i < childs.size(); i++)
    {
        Element child = childs.get(i);
        System.out.print(child.getFirstChildElement("name").getValue());
        System.out.print(" - ");
        System.out.print(getLocationName().getText());
        System.out.print(" - ");
        System.out.println(child.getFirstChildElement("name").getValue().equals(getLocationName().getText()));

        if(child.getFirstChildElement("name").getValue().equals(getLocationName().getText()))
        {
            nodeToDelete = i;
        }
    }

    root.removeChild(nodeToDelete);
    Simplexml sx = new Simplexml();
    sx.save(root, Config.PATH +"incasation.xml");
}
catch(Exception e) {}

我的问题是:

为什么removeChild(i) 不起作用?

编辑: 一周后我找到了解决方案:)

root.removeChild(nodeToDelete);         
Element root2 = (Element) root.copy();

Simplexml sx = new Simplexml();
sx.save(root2, Config.PATH +"incasation.xml");

Java appending XML docs to existing docs

【问题讨论】:

  • 请解释is not working... 另外,nodeToDelete 在传递给 removeChild 之前的值是多少?
  • 我在org.w3c.dom.Element 中看不到任何带有签名 removeChild(int) 的方法,您的 Element 对象的类是什么?
  • class 是 XOM,并且 nodeToDelete int i to n,而its not working 我的意思是没有被删除

标签: java xml


【解决方案1】:

编译时错误? org.w3c.dom.Element#removeChild 需要一个 Node 对象,而不是一个整数值。还是您使用名为Element 的不同类?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-09
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    • 1970-01-01
    相关资源
    最近更新 更多