XML Parse Tree Traversal


 


Processing an Existing XML Document

package jbs.ab3.view;

import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringReader;

import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

public class ViewJdom {

public ViewJdom() {
super();
}

public static void parse ( String xml ) {

System.out.println("XML input to parse: " + xml );

SAXBuilder builder = new SAXBuilder();

InputStream xmlStream = new ByteArrayInputStream(xml.getBytes());

try {

Document document = builder.build( xmlStream );

Element root = document.getRootElement();

move( root );

System.out.println( "XML generated from parse tree: " );

XMLOutputter outputter = new XMLOutputter();
outputter.output( document, System.out );

} catch (JDOMException e) {

System.out.println("JDOMException in ViewJdom.parse" + e.toString() );

} catch (IOException e) {

System.out.println("IOException in ViewJdom.parse" + e.toString() );

}

}

private static void move ( Element element ) {

display( element );

List children = element.getChildren();

if ( children == null ) {

return;

} else {

Iterator childrenIterator = children.iterator();

while ( childrenIterator.hasNext() ) {

Element nextChild = (Element)childrenIterator.next();

move( nextChild );

}

}

}

private static void display ( Element element ) {

System.out.println( "Element name = " + element.getName() );

System.out.println( " attribute = " + element.getAttributes() );

System.out.println( " content = " + element.getContent() );

}

 

}


Results

XML input to parse: <?xml version='1.0' encoding='UTF-8'?>
<view type='login'><heading>Please LogintoAB3</heading>
<content><state></state> <entry><item><label>Login</label><name>loginName</name>
<value>1111</value></item>
<item type='password'><label>Password</label>
<name>loginPassword</name><value></value></item></entry></content>
<message>Error login: fields incomeplete.</message></view>

Element name = view
attribute = [[Attribute: type="login"]]
content = [[Element: <heading/>], [Element: <content/>], [Element: <message/>]]
Element name = heading
attribute = []
content = [[Text: Please Login to AB3]]
Element name = content
attribute = []
content = [[Element: <state/>], [Element: <entry/>]]
Element name = state
attribute = []
content = []
Element name = entry
attribute = []
content = [[Element: <item/>], [Element: <item/>]]
Element name = item
attribute = []
content = [[Element: <label/>], [Element: <name/>], [Element: <value/>]]
Element name = label
attribute = []
content = [[Text: Login]]
Element name = name
attribute = []
content = [[Text: loginName]]
Element name = value
attribute = []
content = [[Text: 1111]]
Element name = item
attribute = [[Attribute: type="password"]]
content = [[Element: <label/>], [Element: <name/>], [Element: <value/>]]
Element name = label
attribute = []
content = [[Text: Password]]
Element name = name
attribute = []
content = [[Text: loginPassword]]
Element name = value
attribute = []
content = []
Element name = message
attribute = []
content = [[Text: Error login: fields incomplete.]]

XML generated from parse tree:
<?xml version="1.0" encoding="UTF-8"?>
<view type="login"><heading>Please Login to AB3</heading><content><state /><entry><item><label>Login</label><name>loginName</name><value>1111</value></item><item type="password"><label>Password</label><name>loginPassword</name><value /></item></entry></content><message>Error login: fields incomplete.</message></view>
 


Run the Application

[Not normally available.]

To see results (on your Tomcat server):

view the console or the catalina.out log