Convert String XML fragment to Document Node in Java

前端 未结 8 2181
迷失自我
迷失自我 2020-11-28 04:00

In Java how can you convert a String that represents a fragment of XML for insertion into an XML document?

e.g.

String newNode =  \"valu         


        
8条回答
  •  难免孤独
    2020-11-28 04:07

    ...and if you're using purely XOM, something like this:

        String xml = "" + xml + "";
        Document doc = new Builder( false ).build( xml, null );
        Nodes children = doc.getRootElement().removeChildren();
        for( int ix = 0; ix < children.size(); ix++ ) {
            otherDocumentElement.appendChild( children.get( ix ) );
        }
    

    XOM uses fakeRoot internally to do pretty much the same, so it should be safe, if not exactly elegant.

提交回复
热议问题