Hello, I have a small problem reading an XML file with Java (using the SAX parser) i use this code
Code:
projetdinamv2 package;
/ **
* <p> Title: SAXParserXML </ p>
*
* <p> Description: Class to read an XML file </ p>
*
<p> * Copyright: Copyright (c) 2008 </ p>
*
* <p> Company: </ p>
*
* @ Author Chetna
* @ Version 1.0
* /
/ / Parse an XML document with JAVA API SAX
/ / importing the necessary API
/ / to the analysis of XML
import org.xml.sax.SAXParseException;
import org.xml.sax .*;
org.xml.sax.helpers.DefaultHandler import;
javax.xml.parsers.SAXParserFactory import;
javax.xml.parsers.ParserConfigurationException import;
javax.xml.parsers.SAXParser import;
/ / to access to files
import java.io. *;
import java.util.ArrayList;
import java.util.Vector;
public class SAXParserXML extends DefaultHandler
(
public SAXParserXML () (
try (
jbInit ();
) Catch (Exception ex) (
ex.printStackTrace ();
)
)
boolean addAuthor;
Author static ArrayList = new ArrayList (); / / dynamic array for storing people
public static void ParcourXML ()
(
DefaultHandler handler = new SAXParserXML ();
SAXParserFactory factory SAXParserFactory.newInstance = ();
try
(
String URI = "http://mydomain.com/project/dblpmomo.xml";
SAXParser saxParser = factory.newSAXParser ();
saxParser.parse (URI handler);
)
catch (Throwable t)
(/ / If it was an error during the analysis
t.printStackTrace ();
System.exit (1);
)
)
/ * Public void error (SAXParseException e) throws SAXParseException
(
throw e;
) * /
public void startDocument () throws SAXException
(
System.out.println ( "beginning of the document");
)
public void endDocument () throws SAXException
(
System.out.println ( "end of document");
)
public void startElement (String namespaceURI, String simpleName, String qualifiedName, Attributes attrs) throws SAXException
(
String qName = simpleName;
if ( "". equals (qName))
(
qName = qualifiedName;
)
if (qName.equalsIgnoreCase ( "author")) / / check whether or not author tag
(
addAuthor = true;
)
if (attrs! = null)
(/ / Attributes listing
if (attrs.getLength ()! = 0)
/ / System.out.println ( "Parameter for" + + qName ":");
for (int i = 0; i <attrs.getLength () i + +)
(
String aName = attrs.getLocalName (i);
/ / get the name of the attribute
if ( "". equals (aName))
(
aName = attrs.getQName (i);
)
)
)
)
public void EndElement (String namespaceURI, String simpleName, String qualifiedName) throws SAXException
(
NomElement String = simpleName;
if (nomElement.equals (""))
(
nomElement = qualifiedName;
)
addAuthor = false;
)
public static void affichageAuthor () / * to read the table of the elements * /
(
System.out.println ( "Displaying the vector table");
for (int i = 0; i <Author.size () i + +)
(
System.out.println (Author.get (i));
)
)
public void characters (char [] ch, int start, int length) throws SAXException
(
String string = new String (ch, start, length);
chaine.trim string = ();
if (! chaine.equals (""))
(
)
if (addAuthor)
(
Author.add (new String (ch, start, length)) / / add attributes in the table of vector
)
)
private void jbInit () throws Exception (
)
) / / End of class
allowing me to read my XML file and store my values (authors) in a table the only problem is that once my program is a tag / authors, it does not continue to read XML!! How? please help
Bookmarks