各位大侠:
我用Java编写Dom的解析程序,总是出现(Unknown Source)。有时在getLength()处,有时在(Element) (nl.item(i))处。
按理说都进到循环内部了,非空判断也过了,不该报错啊。
我找了一下程序没有找到两个线程公用传入参数的情况。
是说Dom内部有什么机制,导致不同的线程、不同的参数,但是在调用这个静态方法的NodeList会相互影响?
如果是怎么避免呢?或者其他什么原因?小弟在此先谢过了~
private static void copyChildren(Element srcTemp, Element des,
Document d) {
 Element src = (Element) srcTemp.cloneNode(true);
 if (src != null) {
  src.normalize();
  if (src.hasChildNodes()) {
    NodeList nl = src.getChildNodes();

    if (nl != null) {
      for (int i = 0; i < nl.getLength(); i++) {
      if (nl.item(i) == null || nl.item(i).getNodeName() == null)
         continue;
      else if (nl.item(i).getNodeName().startsWith("#text")) {
        continue;
      } else {
        Element a = copyElementName((Element) (nl.item(i)), d);
        des.appendChild(a);
        copy((Element) nl.item(i), a, d);
      }
     }
    }
  }
 }
}

解决方案 »

  1.   

    java.lang.NullPointerException
    at com.sun.org.apache.xerces.internal.dom.ParentNode.nodeListGetLength(Unknown Source)
    at com.sun.org.apache.xerces.internal.dom.ParentNode.getLength(Unknown Source)另一次是调用item(Unknown Source)的,找不到了。我看到国外网站上有这样一条信息,我跟大侠确认一下:
    For example, you will run into trouble with NodeLists. Internally 
    they 
    > > cache the length and previously accessed position to improve the 
    > > performance of calls to getLength() and item(i). Invoking these 
    methods 
    > > from multiple threads (even on different NodeLists) without 
    synchronizing 
    > > your application code will lead to the exceptions [1] observed by 
    > > Prashant. You can't count on the other read operations being 
    thread-safe 
    > > either.