English 中文(简体)
清除来自OMD的 no子
原标题:Removing a node from DOM
  • 时间:2011-11-16 12:44:55
  •  标签:
  • java
  • dom

我写了这一方案,以清除树苗,但仍然存在!

如何做到这一点? 在印刷了节点内容之后,它仍然表现出与删除之前相同的内容,这意味着它仍然存在!

法典:

 public class JavaApplication38 {
   public static void check(Node node){


       if (node == null || node.getNodeName() == null)
    return;



  check(node.getFirstChild());  

System.out.println(node.getNodeValue() != null && node.getNodeValue().trim().length() == 0 ? "" : node);
  if (  "abcd".equals(node.getTextContent()))
      node.getParentNode().removeChild(node);
    check(node.getNextSibling());  

}
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {


     File file = new File("d:\a.xml");
  DocumentBuilderFactory dbf =
  DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  Document document = db.parse(file);
  document.getDocumentElement().normalize();




  Node b=document.getFirstChild();


  check(b);
  check(b);

    }

 }
最佳回答

no子被从树中删除,但是它确实是一个 no子,没有父母。 移除儿童之后,再次这样做。

d=document.getElementsByTagName("data1");

b=d.item(0);
System.out.println(b.getTextContent());

你获得相同的文本内容(如果不同的<代码>1 tags有不同的内容)。

问题回答

No, the node is removed.
Removing the node from the tree does automatically delete it.

要想正确清除 no子,你可以撰写一些用于印刷树木的代码。

EDIT: I m not sure if your check method visits every node in the tree.
And I would suggest separating the code that prints the tree from the code that modifies it; then call

  printTree(d);
  modifyTree(d);
  printTree(d);

你们似乎知道并理解再次入侵,因此,你应当能够书写一种使树 print印的方法。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签