给你一段用DOM实现的方法,JDOM我没用过,相信应该差不多吧
/**
* Explore xml tree
* @param childNode
*/
public static void ExploreTree( Node childNode )
{
String strNodeTxt = "";
NodeList childNodeList = null;
String strAttr = "";
switch ( childNode.getNodeType() )
{
case Node.ELEMENT_NODE:
{
int nAttrCount = 0;
System.out.println( childNode.getNodeName() + ": " ); while ( childNode.getAttributes().item( nAttrCount ) != null )
{
strAttr = childNode.getAttributes().item( nAttrCount ).getNodeValue();
System.out.print( childNode.getAttributes().item( nAttrCount ).getNodeName() + ": " );
System.out.println( strAttr );
nAttrCount ++;
} childNodeList = childNode.getChildNodes();
for( int i = 0;i < childNodeList.getLength(); i++ )
{
childNode = childNodeList.item( i );
ExploreTree( childNode );
}
break;
}
case Node.TEXT_NODE:
{
strNodeTxt = childNode.getNodeValue();
if( !strNodeTxt.trim().equals( "" ) )
{
System.out.println( "节点内容:" + strNodeTxt );
}
break;
}
case Node.DOCUMENT_NODE:
{
break;
}
case Node.DOCUMENT_TYPE_NODE:
{
break;
} default :
{
break;
} } // end switch
}
/**
* Explore xml tree
* @param childNode
*/
public static void ExploreTree( Node childNode )
{
String strNodeTxt = "";
NodeList childNodeList = null;
String strAttr = "";
switch ( childNode.getNodeType() )
{
case Node.ELEMENT_NODE:
{
int nAttrCount = 0;
System.out.println( childNode.getNodeName() + ": " ); while ( childNode.getAttributes().item( nAttrCount ) != null )
{
strAttr = childNode.getAttributes().item( nAttrCount ).getNodeValue();
System.out.print( childNode.getAttributes().item( nAttrCount ).getNodeName() + ": " );
System.out.println( strAttr );
nAttrCount ++;
} childNodeList = childNode.getChildNodes();
for( int i = 0;i < childNodeList.getLength(); i++ )
{
childNode = childNodeList.item( i );
ExploreTree( childNode );
}
break;
}
case Node.TEXT_NODE:
{
strNodeTxt = childNode.getNodeValue();
if( !strNodeTxt.trim().equals( "" ) )
{
System.out.println( "节点内容:" + strNodeTxt );
}
break;
}
case Node.DOCUMENT_NODE:
{
break;
}
case Node.DOCUMENT_TYPE_NODE:
{
break;
} default :
{
break;
} } // end switch
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货