-
Java and XML
After what has been said in the title Java and XML, I am having doubt on the parser SAX (Simple API for XML). I mention this because SAX was the first adopted as an XML parser API for XML in Java. So the main reason of posting here is to gather information about the Java and XML related topics. As can be seen along the post, SAX leaves much to be desired with respect to other XML APIs such as DOM-based. Also please provide me some more information about the same.
-
Re: Java and XML
You should know about the example driver which is important from Java and XML point of view.
Example Driver - It is the class that will process each event to launch the SAX processor. Just inherit the default SAX handler DefaultHandler and overwrite the methods for the desired events. In this case I have overwritten the most common:
- startDocument: occurs when you start processing the XML document.
- EndDocument: come at the end of the XML document processing.
- startElement: start occurs when processing an xml tag. This is where you read the tag attributes.
- EndElement: This occurs at the end of processing an xml tag.
- characters: there is to find a text string.
-
Re: Java and XML
Whenever the parser finds a tag will call startElement. Whether the label is. Receive as a parameter the name of the label but not the parent tag. So if we have nested tags with the same name, we must be stored in a persistent object (at least for the processing of the xml document) the route by which we label processing. That is, an object that stores the state in which it is processing the document. This disadvantage is accentuated when reading the labels and values that the characters method is called without specifying the element to which you are accessing. To find out which label is the text we should have passed previously marked by the corresponding startElement. In this case it is essential to the object that marks the state.
-
Re: Java and XML
For this kind of thing say that this API is arcane and outdated. When we see DOM, we will see that you can have random access to any part of the XML document.
Code:
MyDriver.java
org.xml.sax.Attributes import;
import org.xml.sax.SAXException;
org.xml.sax.helpers.DefaultHandler import;
/ **
* SAX event handler example.
*
* @ Author Xela
*
* /
DefaultHandler MyDriver public class extends (
@ Override
public void startDocument () throws SAXException (
System.out.println ("\ nPrinciple document ...");
)
@ Override
public void EndDocument () throws SAXException (
System.out.println ("\ nFin document ...");
)
@ Override
public void startElement (String uri, String localName, String name,
Attributes attributes) throws SAXException (
System.out.println ("\ nProcesando tag ...");
System.out.println ("\ tNamespace uri:" + uri);
System.out.println ("\ tfirst_name:" + localName);
System.out.println ("\ tfirst_name with prefix:" + name);
/ / Loop through the attributes
System.out.println ("\ tProcesando" + attributes.getLength () + "attributes ...");
for (int i = 0; i <attributes.getLength (); i + +) (
System.out.println ("\ t \ tfirst_name:" + attributes.getQName (i));
System.out.println ("\ t \ tValor:" + attributes.getValue (i));
)
// Also we can get the attributes by name
String value = attributes.getValue ("id");
if (value! = null) (
System.out.println ("\ tid:" + value);
)
)
@ Override
public void characters (char [] ch, int start, int length)
throws SAXException (
System.out.println ("\ nProcesando text within a label ...");
System.out.println ("\ TText:" + String.valueOf (ch, start, length));
)
@ Override
public void EndElement (String uri, String localName, String name)
throws SAXException (
System.out.println ("\ nFin tag ...");
System.out.println ("\ tNamespace uri:" + uri);
System.out.println ("\ tfirst_name:" + localName);
System.out.println ("\ tfirst_name with prefix:" + name);
)
)
As you can see the class in this example is very silly. I'll just putting messages to stdout as they jumping events.
Page generated in 1,713,560,730.38173 seconds with 11 queries