com.amx.duet.tools.xml
Class XMLParser

java.lang.Object
  extended bycom.amx.duet.tools.xml.XMLParser

public class XMLParser
extends java.lang.Object

This XMLParser was created as a wrapper class to simplify the parsing of XML files. The user of this class simply needs to create an XMLParser object (hopefully they passed in some sort of file or io stream processing object), and then just call the getNextTag(), getNextTag(String), or getNextNode(String) method to get the tag information that they are searching for.

Here are a few examples on how to create an XMLParser object:

 XMLParser parser = null;
 
 // You can create the parser object using an existing file
 parser = new XMLParser("C:\\DEV\\XMLFile.xml");

 // Or you can create the parser object using an InputStream object
 ByteArrayInputStream stream = new ByteArrayInputStream(xmlBuffer.toString().getBytes());
 parser = new XMLParser(stream);
 
 // Or you could create the parser object with the default constructor and start the parser at a later time
 InputStream stream = new ByteArrayInputStream(xmlBuffer.toString().getBytes());
 parser = new XMLParser();
 if (parser != null)
 	parser.start(stream);
 

These are some examples showing how to use the XMLParser object once it has been created:

 // This will get the Tag values matching the "tag_name"
 if (parser != null)
 {
 	Tag tag = parser.getNextTag("tag_name");
 	String text = "";
 	if (tag != null)
 		text = tag.getText();
 }
 
 // This will give you the next Tag in the XML stream from the stream's current position
 if (parser != null)
 {
 	Tag tag = parser.getNextTag();
 	String text = "";
 	if (tag != null)
 		text = tag.getText();
 }

 // This will give you an XMLNode or a branch of XML tags starting with the one matching your request
 if (parser != null)
 {
 	XMLNode parserNode = parser.getNextNode("tag_name");
 	// At this point you can start traversing the tree starting at the current tag
 	if (parserNode != null)
 		XMLNode root = parserNode.getRoot();
 }
 

Note: If you do not want to change the XML tag stream position, you could create the parser object and then get the root tag like so:

 // This will allow you to walk the XML tree in both forward and reverse order.
 
 // parser creation routine goes here...
 
 if (parser != null)
 {
 	XMLNode parserNode = parser.getNextNode("first_tag"); // whatever your first tag is
 	// At this point you can start traversing the tree starting at the current tag
 	if (parserNode != null)
 		XMLNode root = parserNode.getRoot();
 }
 

If you want to traverse a tag list straight from the XML stream you could do the following:

 XMLNode nextNode = null;
 if (parser != null)
 {
 	while ((nextNode = parser.getNextNode("tags")) != null)
 	{
 		XMLNode tagNode = nextNode.getRoot();
 	}
 }
 // You cannot go back up the XML stream once you make any XMLParse call.
 
Otherwise, see XMLNode for examples on how to traverse the XML node tree.

Since:
AMXTools 1.0.0
Version:
1.0.2
See Also:
XMLNode, Tag, Attribute

Constructor Summary
XMLParser()
          Default constructor
XMLParser(java.io.File file)
          Creates an XML parser with the InputStream taken from File object that was passed in
XMLParser(java.io.InputStream is)
          Initialize the parser to an InputStream object
XMLParser(java.lang.String sFilePath)
          Creates an XML parser with the InputStream taken from File object that was opened from the file path passed in.
 
Method Summary
 int getDebugState()
          Gets the current diagnostic state.
 XMLNode getNextNode()
          Get the next set of tags in the XML file.
 XMLNode getNextNode(java.lang.String sTagName)
          Get the next set of tags in the XML file that match the tag name passed in.
 Tag getNextTag()
          Get the next tag in the XML file.
 Tag getNextTag(java.lang.String sTagName)
          Get the next tag in the XML file that matches the tag name passed in.
 XMLNode getNode(java.lang.String sTagName)
          This method traverses the XML tree from top to bottom until it finds the matching tag name.
 XMLNode getRootNode()
           
 Tag getTag(java.lang.String sTagName)
          This method traverses the XML tree from top to bottom until it finds the matching tag name.
 void setDebug(boolean bState)
          Deprecated. Do not use this method; use setDebugState instead
 void setDebugState(int nState)
          Puts the class into a debug diagnostic state.
 int start(java.io.InputStream is)
          Initialize the parser to an InputStream object
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLParser

public XMLParser()
Default constructor


XMLParser

public XMLParser(java.io.InputStream is)
          throws org.xmlpull.v1.XmlPullParserException,
                 java.io.IOException
Initialize the parser to an InputStream object

Parameters:
is - InputStream

XMLParser

public XMLParser(java.io.File file)
          throws java.io.FileNotFoundException,
                 org.xmlpull.v1.XmlPullParserException,
                 java.io.IOException
Creates an XML parser with the InputStream taken from File object that was passed in

Parameters:
file - File object pointing to the file to read from
Throws:
java.io.FileNotFoundException
org.xmlpull.v1.XmlPullParserException
java.io.IOException

XMLParser

public XMLParser(java.lang.String sFilePath)
          throws java.io.FileNotFoundException,
                 org.xmlpull.v1.XmlPullParserException,
                 java.io.IOException
Creates an XML parser with the InputStream taken from File object that was opened from the file path passed in.

Parameters:
sFilePath - String of the file path to read a file from
Throws:
java.io.FileNotFoundException
org.xmlpull.v1.XmlPullParserException
java.io.IOException
Method Detail

start

public int start(java.io.InputStream is)
          throws org.xmlpull.v1.XmlPullParserException,
                 java.io.IOException
Initialize the parser to an InputStream object

Parameters:
is - InputStream
Returns:
int
Throws:
org.xmlpull.v1.XmlPullParserException
java.io.IOException

getRootNode

public XMLNode getRootNode()
Returns:

getNextNode

public XMLNode getNextNode()
Get the next set of tags in the XML file.

Note: This method updates the xml stream position.

Returns:
XMLNode containg all of the information between the start and end node.

getNextNode

public XMLNode getNextNode(java.lang.String sTagName)
Get the next set of tags in the XML file that match the tag name passed in.

Note: This method updates the xml stream position.

Parameters:
sTagName - String name to search for.
Returns:
XMLNode containg all of the information between the start and end node.

getNode

public XMLNode getNode(java.lang.String sTagName)
This method traverses the XML tree from top to bottom until it finds the matching tag name.

NOTE: This is a slow method due to the process always starting from the ROOT node of the XML tree and working its way down through all of its leafs until it finds the matching text.

If it does not find the matching tag the XML tree pointer will be set to null. In this case, simply call getRootNode() to restart the tree traversal.

Parameters:
sTagName - String name to search for.
Returns:
XMLNode containg all of the information between the start and end node.

getNextTag

public Tag getNextTag()
Get the next tag in the XML file.

Note: This method updates the xml stream position.

Returns:
Tag containg all of the information between the start and end tags.

getNextTag

public Tag getNextTag(java.lang.String sTagName)
Get the next tag in the XML file that matches the tag name passed in.

Note: This method updates the xml stream position.

Parameters:
sTagName - String name to search for.
Returns:
Tag containg all of the information between the start and end tags.

getTag

public Tag getTag(java.lang.String sTagName)
This method traverses the XML tree from top to bottom until it finds the matching tag name.

NOTE: This is a slow method due to the process always starting from the ROOT node of the XML tree and working its way down through all of its leafs until it finds the matching text.

If it does not find the matching tag the XML tree pointer will be set to null. In this case, simply call getRootNode() to restart the tree traversal.

Parameters:
sTagName - String name to search for.
Returns:
Tag containg all of the information between the start and end tags.

setDebug

public void setDebug(boolean bState)
Deprecated. Do not use this method; use setDebugState instead

Parameters:
bState -

setDebugState

public void setDebugState(int nState)
Puts the class into a debug diagnostic state.

Parameters:
nState -

getDebugState

public int getDebugState()
Gets the current diagnostic state.

Returns:


Copyright © 2008 AMX LLC. All Rights Reserved.