RE: RE: parse the child elements of an element
Arathi Arjun <[email protected]> Thu, 23 Oct 2003 22:08:48 -0400
| Newsgroups | gmane.text.xml.sax.general |
|---|---|
| Message-ID | <[email protected]> |
That helps. Also If I want to parse and output automobile and
all its child elements and ignore ship and truck, is there an
easy way to do it other than checking explicitly for
localName.equals("options") and localName.equals("option")like
in the code below the xml file.
<?xml-stylesheet href="mystyle.css" type="text/css"?>
> > <dealership>
> > <automobile make="Buick" model="Century" color="blue">
> > <options>
> > <option>convertible</option>
> > <option>CD Player</option>
> > </options>
> > </automobile>
> > <ship make="sailboat" color="red">
> > </ship>
<truck color="blue">
</truck>
> > </dealership>
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts)
{
if((localName.equals("automobile")||localName.equals("options")||localName.equals("option")
)
{
System.out.println("Start element: " +
localName);
for (int i = 0; i < atts.getLength(); i++)
{
String name = atts.getLocalName(i);
String value = atts.getValue(i);
System.out.println("Attribute: " +
name);
System.out.println("Value: " + value);
}
}
else
super.startElement( namespaceURI, localName,
qName, atts );
}
________________________________________________
Get your own "800" number
Voicemail, fax, email, and a lot more
http://www.ureach.com/reg/tag
---- On Thu, 23 Oct 2003, [email protected] ([email protected])
wrote:
> Your implementation of startElement event should be checking
for "ship"
> nodes then, not "automobile." I think you're prematurely
invoking
> endElement as well.
>
> public void startElement(String namespaceURI, String
localName, String
> qName, Attributes atts)
> {
> if(localName.equals("ship"))
> {
> System.out.println("Start element: " + localName);
> for (int i = 0; i < atts.getLength(); i++)
> {
> String name = atts.getLocalName(i);
> String value = atts.getValue(i);
> System.out.println("Attribute: " + name);
> System.out.println("Value: " + value);
> }
> }
> else
> super.startElement( namespaceURI, localName, qName, atts );
> }
>
> > Hi,
> > In the following XML file:
> >
> > <?xml-stylesheet href="mystyle.css" type="text/css"?>
> > <dealership>
> > <automobile make="Buick" model="Century" color="blue">
> > <options>
> > <option>convertible</option>
> > <option>CD Player</option>
> > </options>
> > </automobile>
> > <ship make="sailboat" color="red">
> > </ship>
> > </dealership>
> >
> > If I want to output the element name and attributes of
> > ship only and ignore automobile with all its child
elements..
> >
> > can I create a filter class that extends XMLFilterImpl
> >
> > and say
> >
> > XMLReader parser;
> >
> > try
> > {
> > parser =
> > (XMLReader)Class.forName(PARSER_NAME).newInstance();
> > filter.setParent(parser);
> > filter.parse(XML-FILE);
> > }
> >
> > and override
> >
> > public void startElement(String namespaceURI, String
localName,
> >
> > String qName, Attributes atts)
> > {
> > if(localName.equals("automobile"))
> > endElement(namespaceURI,localName,qName);
> > else{
> > System.out.println("Start element: " + localName);
> > for (int i = 0; i < atts.getLength(); i++) {
> > String name = atts.getLocalName(i);
> > String value = atts.getValue(i);
> > System.out.println("Attribute: " + name);
> > System.out.println("Value: " + value);
> > }
> > }
> > }
> >
> >
> > That does not skip the child-elements of automobile though.
> > How do I skip the child elements as well....
> >
> >
> >
> > Thanks,
> > Arathi
>
>
>
-------------------------------------------------------
This SF.net email is sponsored by: The SF.net Donation Program.
Do you like what SourceForge.net is doing for the Open
Source Community? Make a contribution, and help us add new
features and functionality. Click here: http://sourceforge.net/donate/
_______________________________________________
List: [email protected]
See: http://www.saxproject.org
https://lists.sourceforge.net/lists/listinfo/sax-users