momo zone

调核人的blog

JDOM与DOM的相互转换

 JDOM与DOM的相互转换

一、DOM2JDOM采用如下方法,不过,这里只能是Document之间的转换:

    public static org.jdom.Document convert(org.w3c.dom.Document domDoc) 
        throws JDOMException, IOException 
{
        
// Create new DOMBuilder, using default parser
        DOMBuilder builder = new DOMBuilder();
        org.jdom.Document jdomDoc 
= builder.build(domDoc);
        
return jdomDoc;
    }

二、JDOM2DOM,分别采用如下方法:
1、Document之间的转换:

public static org.w3c.dom.Document JDomDoc2Dom(org.jdom.Document jdomDoc) throws Exception {
        DOMOutputter outputter 
= new DOMOutputter();
        
return outputter.output(jdomDoc);
}

2、Element之间的转换,采用如下两个方法,演示的是将一个jdom的Element转换成w3c的Element,并将其附加到指定的W3C的Document下:

    /** *//**
     * 将由JDOM返回的如下结果ELEMENT:
     * <Response>
     *     <Data>
     *         <Record>
     *             <id/>
     *             </name>
     *         </Record>
     *     </Data>
     * </Response>
     * 转换到由w3c的Document中
     * 
@param doc    w3c的Document
     * 
@param element    JDom的Element
     * 
@return    由JDom返回结果Element转换到w3c Document Element并附加到其指定的路径
     * 
@throws DOMException
     * 
@throws Exception
     
*/

    
public static Document convert(org.w3c.dom.Document doc,org.jdom.Element element) throws DOMException, Exception{
        org.w3c.dom.Element root
=null,response=null;
        
int layer=1;
        root 
= doc.getDocumentElement();
        response
=convertElement2Dom(doc,element,layer);
        
//System.out.println("convertElement2Dom结果:"+XmlUtil._node2String(response));
        root.getElementsByTagName("data").item(0).appendChild((Node)response);
        
return doc;
    }

    
/** *//**
     * 将JDom的Element转换成org.w3c.dom.Element
     * 
@param doc org.w3c.dom.Document
     * 
@param element org.jdom.Element
     * 
@param layer 用于判断是否第一层,第一层不用其自身的名称创建结点,用Response代替,只起判断做用
     * 
@return org.w3c.dom.Element
     * 
@throws DOMException
     * 
@throws Exception
     
*/

    
private static org.w3c.dom.Element convertElement2Dom(org.w3c.dom.Document doc,org.jdom.Element element,int layer) throws DOMException, Exception{
        org.w3c.dom.Element element3c
=null;
        
if(layer==1){
            element3c
=doc.createElement("Response");
        }
else{
            element3c
=doc.createElement(element.getName());
        }

        layer
++;
        List listChildren
=element.getChildren();
        String elename
="";
        String elevalue
="";
        
if(listChildren.size()>0){//有下级节点
            List listElement=element.getContent();
            
for(int i=0;i<listElement.size();i++){
                org.jdom.Element tmpElement
=(org.jdom.Element)listElement.get(i);
                element3c.appendChild(convertElement2Dom(doc,tmpElement,layer));
                
            }

        }
else{//无下级节点
            elename=element.getName();//名称
            elevalue=element.getText();//
            element3c=doc.createElement(elename);
            BNXmlUtil.setNodeValue(element3c, elevalue);
//给结点赋值(BNXmlUtil为普元EOS类,可换其它设值)
            List eleAttr=element.getAttributes();
            
for(int i=0;i<eleAttr.size();i++){//增加赋性值
                Attribute attr=(Attribute)eleAttr.get(i);
                String attrName
=attr.getName();
                String attrValue
=attr.getValue();
                element3c.setAttribute(attrName, attrValue);
            }

            
//System.out.println("名称:"+elename+"-值:"+elevalue);
            
            
        }

        
return element3c;
    }

参考:
http://www.ibm.com/developerworks/java/library/x-tipcdm.html (Converting from DOM)
http://www.ibm.com/developerworks/xml/library/x-tipcjdm.html (Converting from JDOM)

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: