|
| ||||||||||
| Tags: java, override, parser, sax, xml |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| Java and XML
|
|
#2
| ||||
| ||||
| 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:
|
|
#3
| ||||
| ||||
| 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. |
|
#4
| |||
| |||
| 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);
)
) |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "Java and XML" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| JAVA/Stutter.J.2 is a virus or any kind of java file | GaganjyotTechie | Networking & Security | 6 | 29-05-2011 09:55 AM |
| Setting Of Java to The Point At Manual Java | winni | Software Development | 4 | 10-01-2011 09:05 PM |
| Java Programming using Adventnet SNMP Java API | ROCKING_Suhas | Software Development | 5 | 17-07-2010 06:52 AM |
| Comparison between core java and advanced java | Prashobh Mallu | Software Development | 5 | 19-01-2010 09:57 AM |
| Link List Example in Java Sample program in Java | trickson | Software Development | 2 | 04-08-2009 08:23 PM |