|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.amx.duet.tools.xml.XMLParser
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 theTag
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 nextTag
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 anXMLNode
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.
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 |
public XMLParser()
public XMLParser(java.io.InputStream is) throws org.xmlpull.v1.XmlPullParserException, java.io.IOException
is
- InputStreampublic XMLParser(java.io.File file) throws java.io.FileNotFoundException, org.xmlpull.v1.XmlPullParserException, java.io.IOException
file
- File object pointing to the file to read from
java.io.FileNotFoundException
org.xmlpull.v1.XmlPullParserException
java.io.IOException
public XMLParser(java.lang.String sFilePath) throws java.io.FileNotFoundException, org.xmlpull.v1.XmlPullParserException, java.io.IOException
sFilePath
- String of the file path to read a file from
java.io.FileNotFoundException
org.xmlpull.v1.XmlPullParserException
java.io.IOException
Method Detail |
public int start(java.io.InputStream is) throws org.xmlpull.v1.XmlPullParserException, java.io.IOException
is
- InputStream
org.xmlpull.v1.XmlPullParserException
java.io.IOException
public XMLNode getRootNode()
public XMLNode getNextNode()
Note: This method updates the xml stream position.
XMLNode
containg all of the information between the start and end node.public XMLNode getNextNode(java.lang.String sTagName)
Note: This method updates the xml stream position.
sTagName
- String name to search for.
XMLNode
containg all of the information between the start and end node.public XMLNode getNode(java.lang.String sTagName)
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.
sTagName
- String name to search for.
XMLNode
containg all of the information between the start and end node.public Tag getNextTag()
Note: This method updates the xml stream position.
Tag
containg all of the information between the start and end tags.public Tag getNextTag(java.lang.String sTagName)
Note: This method updates the xml stream position.
sTagName
- String name to search for.
Tag
containg all of the information between the start and end tags.public Tag getTag(java.lang.String sTagName)
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.
sTagName
- String name to search for.
Tag
containg all of the information between the start and end tags.public void setDebug(boolean bState)
bState
- public void setDebugState(int nState)
nState
- public int getDebugState()
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |