开发者

how to using DOM importNode without DOMException

I want to copy an node to a different Documentg, but it always has DOME开发者_开发技巧xception about

org.apache.harmony.xml.dom.NodeImpl.setNameNS(NodeImpl.java:227)

here is my code

private String getString(Node seqNode) {
    try {           
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = factory.newDocumentBuilder();          
        Document doc = docBuilder.newDocument();

        Element root = doc.createElement("Test");
        doc.appendChild(root);

        /* following line will cause DOMException */
        doc.importNode(seqNode, true);
        ...
        ...
       } catch (Exception e) {

       }
}

where seqNode belongs to other Document

does any body has idea about this issue? :-)


I ran into this problem too. I was getting this exception when calling either importNode() or cloneNode(). And BTW, the XML I was parsing/generating was not using namespaces.

It seems that the DOM parser (from Apache Harmony) that is included in Android is buggy. See this link: Issue 2735: Harmony DOM implementation is buggy and noncompliant. Everything works fine if the same code is executed using plain Java 1.6 (which isn't based on Harmony of course).

I tried setting setNamespaceAware(true) on the DocumentBuilder, but this did not help.

Eventually, I gave up and worked around the issue by using adoptNode() rather than importNode(). This is kind of incestuous, because it is stealing a node from one Document tree and putting it into another. But in my case, the first Document tree was only temporary, so I could do things this way.


I'm guessing, but it seems that you are trying to import node with namespace defined, where your target document does not have this namespace declared.

So, what namespaces are declared in source document? Did you declare any namespaces in target document?


The input is a smil String shown below:

 <smil>
   <head>
     <layout>
       <root-layout height="720" width="1280"/>
       <transition id="fade" type="fade" subtype="crossfade" dur="1s"/>
       <region id="_33_32_bkgd_image" left="0" top="0" width="1280" height="720" background-color="#c12121" showBackground="whenActive" z-index="0"></region>
       <region id="_33_32_I001" left="380" top="27" width="405" height="352" z-index="1"></region><region id="_33_32_I002" left="0" top="365" width="354" height="354" z-index="2"></region>
     </layout>
   </head>
   <body>
     <seq begin="wallclock(2011-09-22T01:52:00)" end="wallclock(2011-09-23T00:00:00)">
       <par dur="10s" xml:id="32" repeatCount="1">
         <brush color="#c12121" region="_33_32_bkgd_image"></brush>
         <seq repeatCount="indefinite">
            <img xml:id="30" region="_33_32_I001" src="http://127.0.0.1/Service/User/2_user/Media/Image/30_image.jpg?JFBukihsTu" dur="5s" fit="meet" regPoint="center" regAlign="center">          
              <metadata xml:id="meta-rdf">            
                <meta name="MD5" content="7c8b59b28ea2247f20bc538dcb7108f3"></meta><meta name="width" content="531"></meta><meta name="height" content="720"></meta></metadata></img>

           <img xml:id="27" region="_33_32_I001" src="http://127.0.0.1/Service/User/2_user/Media/Image/27_image.jpg?jTqCMuIxsX" dur="5s" fit="meet" regPoint="center" regAlign="center">          
             <metadata xml:id="meta-rdf">            
                <meta name="MD5" content="db51409f243f79c566811d1b307a77a1"></meta><meta name="width" content="427"></meta><meta name="height" content="602"></meta></metadata></img>
        </seq>
      </par>
     </seq>
   </body>
 </smil>

and the original Document is generated by:

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document dom = builder.parse(new ByteArrayInputStream(smil.getBytes()));

and the seqNode represents the "seq" node (the child of body tag)

I want to copy "seq" and all of it's childs to new Document

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜