我没看到代码XMLDemo如何显示所有节点?
// Constants -----------------------------------------------------------------------------------
private static final int _tab = 4;
/**
* This constructor parses the XML file into a W3C DOM document, and displays it
* on the screen.
*
* @see Document
* @see DocumentBuilder
* @see DocumentBuilderFactory
*/
XMLDemoScreen()
{
setTitle( new LabelField( "XML Demo" , LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH ) );
try
{
// Build a document based on the XML file.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream inputStream = getClass().getResourceAsStream( _xmlFileName );
Document document = builder.parse( inputStream );
// Normalize the root element of the XML document. This ensures that all Text
// nodes under the root node are put into a "normal" form, which means that
// there are neither adjacent Text nodes nor empty Text nodes in the document.
// See Node.normalize().
Element rootElement = document.getDocumentElement();
rootElement.normalize();
// Display the root node and all its descendant nodes, which covers the entire
// document.
displayNode( rootElement, 0 );
}
catch ( Exception e )
{
System.out.println( e.toString() );
}
}
/**
* Displays a node at a specified depth, as well as all its descendants.
*
* @param node The node to display.
* @param depth The depth of this node in the document tree.
*/
private void displayNode( Node node, int depth )
{
// Because we can inspect the XML file, we know that it contains only XML elements
// and text, so this algorithm is written specifically to handle these cases.
// A real-world application will be more robust, and will handle all node types.
// See the entire list in org.w3c.dom.Node.
// The XML file is laid out such that each Element node will either have one Text
// node child (e.g. <Element>Text</Element>), or >= 1 children consisting of at
// least one Element node, and possibly some Text nodes. Start by figuring out
// what kind of node we're dealing with.
if ( node.getNodeType() == Node.ELEMENT_NODE )
{
StringBuffer buffer = new StringBuffer();
indentStringBuffer( buffer, depth );
NodeList childNodes = node.getChildNodes();
int numChildren = childNodes.getLength();
Node firstChild = childNodes.item( 0 );
// If the node has only one child and that child is a Text node, then it's of
// the form <Element>Text</Element>, so print 'Element = "Text"'.
if ( numChildren == 1 && firstChild.getNodeType() == Node.TEXT_NODE )
{
buffer.append( node.getNodeName() ).append( " = \"" ).append( firstChild.getNodeValue() ).append( '"' );
add( new RichTextField( buffer.toString() ) );
}
else
{
// The node either has > 1 children, or it has at least one Element node child.
// Either way, its children have to be visited. Print the name of the element
// and recurse.
buffer.append( node.getNodeName() );
add( new RichTextField( buffer.toString() ) );
// Recursively visit all this node's children.
for ( int i = 0; i < numChildren; ++i )
{
displayNode( childNodes.item( i ), depth + 1 );
}
}
}
else
{
// Node is not an Element node, so we know it is a Text node. Make sure it is
// not an "empty" Text node (normalize() doesn't consider a Text node consisting
// of only newlines and spaces to be "empty"). If it is not empty, print it.
String nodeValue = node.getNodeValue();
if ( nodeValue.trim().length() != 0 )
{
StringBuffer buffer = new StringBuffer();
indentStringBuffer( buffer, depth );
buffer.append( '"' ).append( nodeValue ).append( '"' );
add( new RichTextField( buffer.toString() ) );
}
}
}
/**
* Adds leading spaces to the provided string buffer according to the depth of
* the node it represents.
*
* @param buffer The string buffer to add leading spaces to.
* @param depth The depth of the node the string buffer represents.
*/
private static void indentStringBuffer( StringBuffer buffer, int depth )
{
int indent = depth * _tab;
for ( int i = 0; i < indent; ++i )
{
buffer.append( ' ' );
}
}
}
我试图理解它,但我认为循环只能显示一个节点的子节点而不是所有节点。 例如,在此代码上
<annuaire>
<personne >
<image>p0.jpg</image>
<nom>test0</nom>
<tirtre>test0</tirtre>
<Tel>5555550</Tel>
<email>test0@xml.ma</email>
</personne>
<personne >
<image>p1.jpg</image>
<nom>test</nom>
<tirtre>test</tirtre>
<Tel>555555</Tel>
<email>test@xml.ma</email>
</personne>
</annuaire>
我认为代码只显示孩子们的第一个节点人物。
因为我需要在RichList上显示它们我试图从黑莓上给出的这个样本中获得灵感。 但我不知道我是怎么理解代码的。 非常感谢你的帮助。