How to use sax xml parser

hi was wondering if someone could give me an example on how to use sax
and a document handler and explain a bit

to use sax parsers you need basically to extends handlerBase wich will force you to have the 3 following methods
// imports for the sax portion of the manager,
// the portion that will permit the interpretation of
import com.sun.xml.parser.Resolver;
import org.xml.sax.*;
import org.xml.sax.helpers.ParserFactory;
public class XMLManager extends HandlerBase
private String currentElement = null; //current element name for parsing
* Default constructor
public XMLManager()
/*SAX METHODS*/
* This method is called when the SAX parser encounters an open element
* tag. Must remember which element tag was just opened (so that the
* characters(..) method can do something useful with the data that is
* read by the parser.
public void startElement(String name,AttributeList atts){
if( name.equalsIgnoreCase("MY XML TAG NAME") )
currentElement = "MY XML TAG NAME";
//then do stuff for that type of tag
* This method is called when the SAX parser encounters a close element
* tag. If the message tag is closed it means the message is valid and ready
* to be treated
public void endElement(String name){
currentElement = "";
* This method is called when the SAX parser encounters #PCDATA or CDATA.
* It is important to remember which element tag was just opened so that
* this data can be put in the right object or variable.
* Also the start index and length integer must be used to retrieve only
* a portion of the data stored in the char[]
* This section will only contain treatment for tags wich have content
* between start and end tags
public void characters(char ch[],int start,int length){
//dont read ch[] , use the
//range provided by the SAX parser.
String value = new String(ch,start ,length);
Once you have defined these 3 methods, define a method called for example parseIt(String mss) wich will contain the following code to parse the content of a string mss, a string containing xml data
try
//create an InputSource from the XML source received
StringReader r = new StringReader(mss);
InputSource is = new InputSource( r );
//create a SAX parser using SAX interfaces and classes
String parserClassName = "com.sun.xml.parser.Parser";
org.xml.sax.Parser parser = org.xml.sax.helpers.ParserFactory.makeParser(parserClassName);
//create document handler to do something useful
//with the XML document being parsed by the parser.
parser.setDocumentHandler(this);
parser.parse(is);
catch(Throwable t){System.out.println(t);t.printStackTrace();}
you can't fail with this ;)

Similar Messages

  • HOW TO: Use the XML parser in Oracle 8.1.7

    I am trying to figure out how to use the xml parser provided in oracle 8.1.7. all i want to do is parse a xml report that is defined using a schema, and place the data into the proper tables. i am totally unfamiliar with the xml parser and how it works. i have done some reading on the subject, but seem to be getting some conflicting infromation about which utilites i need and how to invoke them. can someone please tell me what utilities i need, how to invoke them, and what i need to do to get a xml document to parse and insert to a table? I would greatly appreciate any help anybody could offer. thanks.

    You can parse the XML Document with XML Parser and place the data into database using XSU(XML SQL Utility).
    Both of these are included in XDK for Java at:
    http://otn.oracle.com/tech/xml/xdk_java
    The following document could also help:
    Oracle9i XML Developer's Guide--XDK [PDF] at http://otn.oracle.com/tech/xml/doc.html

  • Sax XML Parser? URLDecoder.decode bad in applet? Explorer Browser jvm?

    Ok, here it goes folks, I have a serious problem with Explorers jvm, I'm developing using jdk 1.3.1 to write swing applets, in anyevent little functions that I use will not work with Explorers Browser, for example, the Explorer JVM understands the Vector addElement function, but not the Vector add function? Also, It doesn't recognize the HashMap class? Ok, is there anyway to control the JVM used with Explorer? Ok, but here goes my real question?
    I am writing an applet/servlet that interfaces with an oracle database. The data is returned in the form of xml. We are using the Sax XML parser interface. The parser code previously in the servlet(server side) and worked fine, but I've now moved the parsing code to the applet side, however, it seems to not want to work on the applet side. It simply hangs? I think it's a jvm issue, I've been getting many hangs with seemingly standard classes and methods, such as URLDecoder.decode(). Anyone else run into this problem using the Sax XML Parser, I have no idea what classes in the SAX Parser could be giving us a problems, or if that is even my issue. Anyone else using SAX XML parsing in an applet with Explorer 5.5. or 5.0, please let me know, if you've had issues?
    Sorry, to be so long winded, and not explaining the problem very well.

    First, get Sun's Java Plug-in from http://java.sun.com/products/plugin/index-1.4.html
    and read up on how to make your browser use it instead.

  • How to use SAX parser in J2ME

    Please help me how to use SAX parser in J2ME.
    Is there any function to find the value of a particular element from a XML file?
    I am able to get Element name, Values, Attributes. But control is not in my hand, DefaultHanldler automatically invokes Character method, then only I am able to get values. But I don't know when this method gets invoked.
    Is there any way or method so that I can get value of any element or attribute just by passing element name as parameter in SAX parser?
    Is there any other parser through which I can perform this task in J2ME?
    Thanks in advance.

    Hi..
    have a look at this.
    http://www-128.ibm.com/developerworks/library/wi-parsexml/
    MeTitus

  • Problem for using oracle xml parser v2 for 8.1.7

    My first posting was messed up. This is re-posting the same question.
    Problem for using oracle xml parser v2 for 8.1.7
    I have a sylesheet with
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">.
    It works fine if I refer this xsl file in xml file as follows:
    <?xml-stylesheet type="text/xsl" href="http://...../GN.xsl"?>.
    When I use this xsl in pl/sql package, I got
    ORA-20100: Error occurred while processing: XSL-1009: Attribute 'xsl:version' not found in 'xsl:stylesheet'.
    After I changed name space definition to
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> in xsl file, I got
    ORA-20100: Error occurred while processing: XSL-1019: Expected ']' instead of '$'.
    I am using xml parser v2 for 8.1.7
    Can anyone explain why it happens? What is the solution?
    Yi

    <BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>Originally posted by Steven Muench ([email protected]):
    Element's dont have text content, they [b]contain text node children.
    So instead of trying to setNodeValue() on the element, construct a Text node and use the appendChild method on the element to append the text node as a child of the element.<HR></BLOCKQUOTE>
    Steve,
    We are also creating an XML DOM from java and are having trouble getting the tags created as we want. When we use XMLText it creates the tag as <tagName/>value rather than <tagName>value</tagName>. We want separate open and close tags. Any ideas?
    Lori

  • How to use the kxml parser?

    I am getting problem in using the kxml parser.Actually my application is showing the java.lang.NoClassDefFoundError: org/kxml/parser/XmlParser error.
    Can you tell me how to use the kxml parser with our j2me application or what are the steps for this?
    Whether we have to use the kxml.zip or some other JAR file for this?
    Again whether we have to set any classpath for this?

    I was getting same error using kxml parser. The reason is losing interface in which constants are declared.
    You need to correct code of kxml a bit.
    In every place where constant from XmlPullParser interface is referenced only by its name (without reference to its class XmlPullParser) you need to change for use full name. it means changes like:
    START_DOCUMENT to XmlPullParser.START_DOCUMENT
    Everywhere.
    That's all!

  • XML validation using Oracle XML parser v2

    Not sure if this is the right forum for this question as I didn't found any.
    I had a tough time trying to build an XMLSchema (Oracle XML Parser V2) object from the input schema having include/import. Actually I am working on a Java program to validate the input xml with input schema using Oracle XML parser v2 api.
    The issue is that the code doesn't work for schema that has some "include" in it. The code is working fine for the schema without any import/include. Here is the lines of code -
    XSDBuilder builder = new XSDBuilder();
    schema = builder.build(new InputSource(xsdReader));
    I am writing this in JDeveloper. The error is -
    oracle.xml.parser.schema.XSDException: Can not build schema 'http://www.fpml.org/2005/FpML-4-2' located at 'xsd\FpML42\xml\fpml-fx-4-2.xsd'
    I also tried creating the EntityResolver to specify the path of included schema. But that doesn't solves the problem.
    I am not sure whether parser is able to locate the included schema or not.The main schema and included schema are all in same folder. The included schema is perfectly fine and has no errors.
    Please help if you have encountered this issue and resolved it. I will really appreciate any pointers or clue to solve this issue.
    Thanks.

    Thanks for the reply. But it does not seem to be working. Am I missing something
    I put <TrxDate xsi:nil="true"/> but still getting error.
    Here's my xsd:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified">
    <xsd:element name="TrxDate" type="xsd:date" nillable="true" />
    XML:
    <?xml version="1.0" encoding="UTF-8" ?>
    <ServiceRequest>
    <TrxDate nil="true"/>
    </ServiceRequest>

  • How to use SAX Parseto paser xml without root element

    Hello,
    I have big a xml file(500M) with without root element. How can I use SAXParse to parse it.

    A well-formed XML file has a single root element. Your parser can't deal with any other kind. So find the person who produced that file and explain politely that it's no good. (Hitting them with a big stick would be optional.)
    You could write a non-XML program that reads it in and creates a new version, wrapped in a single root element, if your supplier can't fix their problem.

  • How to download sun XML parser/Where to find the parser itself to downloa?.

    I saw the website below. But I do not have a clue of what to download and where to store any files in my directories and how to set the classpaths to run the file below.
    http://java.sun.com/developer/earlyAccess/xml/index.html
    to run this program
    import java.io.File;
    import com.sun.xml.parser.Resolver;
    import com.sun.xml.tree.XmlDocument;
    import org.xml.sax.InputSource;
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXParseException;
    public class Simpleparser
    public static void main (String argv [])
         InputSource input;
    XmlDocument doc;
    }

    The date on that page is 2002-10-14. It is totally obsolete. If you saw examples using a parser whose name starts with "com.sun" then those examples are totally obsolete as well.
    If you're using Java 1.4 then it already includes an XML parser. Just look for Java XML tutorials that are more recent and read them. You don't need to download anything.

  • Is it possible to configure CF10 or CF11 to use Xerces XML parser instead of Saxon XML parser?

    Could anyone tell me if it is possible to configure CF10 or CF 11 to use the older Xerces XML parser instead of the Saxon XML parser.
    I am in the process of migrating a website from CF8 to CF11. Several sections rely on XML transformation, which no longer work in CF11. After investigating the problem it seems to be that the Saxon Parser is more strict, causing these errors.

    Well I guess Parsers would be better solution. As u said u need to insert node at specific location.
    so now u have to decide which parser u need to choose according to u r requirmnet. DOM or SAX maily.
    Both has adv and di-advs.
    ...yogesh

  • SAX XML Parsing and Garbage Collection

    I'm writing an XML parser that is running extremely slow while parsing large documents, I believe due to garbage collection. The implementation just overloads the DefaultHandler and hands that to a SAX Parser. This works fine on 2500 line XML document, taking on average a second or two, but becomes untennable when the document size is increased by a factor of 10 (often taking 5+ minutes).
    I come from a C++ background and haven't dealt with automatic garbage collection at an expert level, so I'd appreciate any help. Below is a snapshot garbage collection done while a 25000 line document being parsed. It looks to me as the young objects are staying small, but older objects are going between 13M and 90M and back again. Is that what's happening? Can anyone give me leads as to what to look? Thanks in advance.
    [GC [DefNew: 6234K->0K(7168K), 0.0057359 secs] 15517K->13022K(101696K), 0.0057907 secs]
    [GC [DefNew: 6235K->0K(7168K), 0.0057748 secs] 19257K->16762K(101696K), 0.0060357 secs]
    [GC [DefNew: 6235K->0K(7168K), 0.0023584 secs] 22997K->18009K(101696K), 0.0024123 secs]
    [GC [DefNew: 6236K->0K(7168K), 0.0040785 secs] 24245K->20502K(101696K), 0.0041315 secs]
    [GC [DefNew: 4990K->0K(7168K), 0.0041025 secs] 25492K->22996K(101696K), 0.0041544 secs]
    [GC [DefNew: 4990K->0K(7168K), 0.0041486 secs] 27987K->25491K(101696K), 0.0042022 secs]
    [GC [DefNew: 4991K->0K(7168K), 0.0040782 secs] 30481K->27985K(101696K), 0.0041299 secs]
    [GC [DefNew: 4991K->0K(7168K), 0.0040935 secs] 32976K->30479K(101696K), 0.0046294 secs]
    [GC [DefNew: 4991K->0K(7168K), 0.0041433 secs] 35471K->32974K(101696K), 0.0041958 secs]
    [GC [DefNew: 4992K->0K(7168K), 0.0041114 secs] 37966K->35469K(101696K), 0.0041634 secs]
    [GC [DefNew: 6240K->0K(7168K), 0.0024190 secs] 41709K->36717K(101696K), 0.0024729 secs]
    [GC [DefNew: 6240K->0K(7168K), 0.0041377 secs] 42957K->39212K(101696K), 0.0041972 secs]
    [GC [DefNew: 4993K->0K(7168K), 0.0041234 secs] 44205K->41707K(101696K), 0.0041762 secs]
    [GC [DefNew: 4993K->0K(7168K), 0.0042827 secs] 46701K->44203K(101696K), 0.0046299 secs]
    [GC [DefNew: 4994K->0K(7168K), 0.0043195 secs] 49197K->46699K(101696K), 0.0043751 secs]
    [GC [DefNew: 4994K->0K(7168K), 0.0042740 secs] 51693K->49195K(101696K), 0.0043293 secs]
    [GC [DefNew: 4995K->0K(7168K), 0.0042044 secs] 54190K->51692K(101696K), 0.0042575 secs]
    [GC [DefNew: 4995K->0K(7168K), 0.0042237 secs] 56686K->54188K(101696K), 0.0042762 secs]
    [GC [DefNew: 4996K->1K(7168K), 0.0040036 secs] 59183K->56685K(101696K), 0.0040550 secs]
    [GC [DefNew: 4996K->1K(7168K), 0.0042095 secs] 61680K->59182K(101696K), 0.0045992 secs]
    [GC [DefNew: 4997K->1K(7168K), 0.0042193 secs] 64178K->61679K(101696K), 0.0042715 secs]
    [GC [DefNew: 4997K->1K(7168K), 0.0042301 secs] 66675K->64176K(101696K), 0.0042818 secs]
    [GC [DefNew: 4997K->1K(7168K), 0.0042726 secs] 69173K->66674K(101696K), 0.0043251 secs]
    [GC [DefNew: 4998K->1K(7168K), 0.0042564 secs] 71671K->69172K(101696K), 0.0043084 secs]
    [GC [DefNew: 4998K->1K(7168K), 0.0040301 secs] 74169K->71669K(101696K), 0.0040812 secs]
    [GC [DefNew: 4999K->1K(7168K), 0.0042656 secs] 76668K->74168K(101696K), 0.0047255 secs]
    [GC [DefNew: 4999K->1K(7168K), 0.0043100 secs] 79166K->76666K(101696K), 0.0043737 secs]
    [GC [DefNew: 5000K->1K(7168K), 0.0043492 secs] 81665K->79165K(101696K), 0.0044034 secs]
    [GC [DefNew: 5000K->1K(7168K), 0.0043165 secs] 84164K->81664K(101696K), 0.0043693 secs]
    [GC [DefNew: 5001K->1K(7168K), 0.0044196 secs] 86663K->84163K(101696K), 0.0044735 secs]
    [GC [DefNew: 5001K->1K(7168K), 0.0044439 secs] 89163K->86662K(101696K), 0.0044967 secs]
    [GC [DefNew: 5001K->1K(7168K), 0.0043983 secs] 91662K->89161K(101696K), 0.0045313 secs]
    [GC [DefNew: 5002K->1K(7168K), 0.0043422 secs] 94162K->91661K(101696K), 0.0043944 secs]
    [GC [DefNew: 5002K->1K(7168K), 0.0044223 secs] 96662K->94160K(101696K), 0.0044757 secs]
    [GC [DefNew: 5002K->1K(7168K), 0.0048545 secs][Tenured: 96659K->9291K(97032K), 0.0863445 secs] 99162K->9291K(104200K), 0.0913954 secs]

    I don't think I'm creating megabytes of data, not using hashes or other expandable arrays. I've copied and pasted some code from my overriding of a DefaultHandler to give better definition to the problem.
    I'm working with TPTP right now as a profiler. Are there any others you'd recommend?
         public void startElement(String uri, String localname, String qName, Attributes attribs)
         throws SAXException
              if(qName.equals("Line"))
                   attributes = attribs;
              else if(qName.equals("Note"))
                   attributes = attribs;
              else if(qName.equals("Selection"))
                   String idValue = attribs.getValue("id");
                   String oValue = attribs.getValue("off");
                   String lValue = attribs.getValue("len");
                   if(idValue != null && oValue != null && lValue != null)
                        if(isImport)
                             issueSelectionContainer.addUnloadedSelection(Integer.parseInt(idValue), Integer.parseInt(oValue), Integer.parseInt(lValue));
                        else
                             issueSelectionContainer.addSelection(Integer.parseInt(idValue), Integer.parseInt(oValue), Integer.parseInt(lValue));
              else if(qName.equals("Issue"))
                   attributes = attribs;
              if(qName.equals("IsFinal"))
                   isFinal = true;
         public void endElement(String uri, String localname, String qName)
         throws SAXException
              if(textElement.length() > 0)
                   if(qName.equals("Line"))
                        String qmValue = attributes.getValue("qm");
                        String lnValue = attributes.getValue("ln");
                        String pgValue = attributes.getValue("pg");
                        boolean qm = (qmValue != null && qmValue.equalsIgnoreCase("true"));
                        short ln = (lnValue != null) ? Short.parseShort(lnValue) : 0;
                        short pg;
                        if(pgValue != null)
                             pg = Short.parseShort(pgValue);
                             page = pg;
                        else
                             pg = page;
                        lineContainer.addLine(textElement + '\n', ln, pg, qm, null);
                   else if(qName.equals("Note"))
                        String lnValue = attributes.getValue("line");
                        if(lnValue != null)
                             int line = Integer.parseInt(lnValue);
                             try
                                  lineContainer.getLine(line).setNote(textElement);
                             catch (IndexOutOfBoundsException e)
                                  throw new SAXException("XML/Data mismatch.  Note on line \"" + line + "\" out of \"" + lineContainer.lineCount() + "\" lines", e);
                   else if(qName.equals("Issue") && isImport)
                        String idValue = attributes.getValue("id");
                        String cValue = attributes.getValue("color");
                        if(idValue != null && cValue != null)
                             issueSelectionContainer.addUnloadedIssueSelection(Integer.parseInt(idValue), new Color(Integer.parseInt(cValue)), textElement);
                   else if(qName.equals("Name"))
                        name = textElement;
                   else if(qName.equals("Description"))
                        description = textElement;
                   else if(qName.equals("CreationDate"))
                        date = getCalendarFromString(textElement);
                   textElement = "";
                   attributes = null;
         public void characters(char buf[], int offset, int len)
        throws SAXException
              textElement = new String(buf, offset, len);
        }

  • Using a xml parser with jbuilder

    I'm using jbuilder and the required librairies I've just added require me to add an XML parser to the path. I've just downloaded one from www.apache.org.
    How do I incorporate this parser into my project. I need to use the following packages but I'm not sure how I can put everything together.
    If you understand what my problem is please help. Thanks , B.
    import org.w3c.dom.Document;
    import org.w3c.dom.DOMException;

    Go to tools | configure libraries.
    click the new button on the left
    Type in the name you want the library to be identified with (XML Parser or something)
    Choose the location (this determines how many people and projects have access to the libraries)
    Click the add button and find the *.jar files.
    Ok back out of the dialogs
    Now select the Project Properties (or default props to have the library available to all projects, not recommended tho)
    Select the paths tab
    Click Add and select the Library you added in the earlier steps.
    If you have any probs, www.borland.com has heaps of stuff on configuring libraries

  • Parsing errors with CDATA tags using oracle xml parser v2

    I'm using the oracle.xml.parser.v2 parser to combine a
    generated xml document from a database with a static
    xsl file to produce html. Within the xml document, there
    are fairly large CDATA sections (500 lines) which happen
    to contain javaScript.
    Occasionally, I'm getting xml tags in the final html
    document! It seems that the oracle parser pukes
    on a certain type of data within a CDATA tag, and then
    replaces an angle bracket (<) of a tag with a #60. This
    can cause html tags being viewed in the document. At
    first, I thought the amount of JavaScript within the
    CDATA tag was too large for the parser to handle,
    because when I split it up into 2 or more parts the errors
    went away. But, re-arranging the JavaScript (String in the CDATA tag)
    can make the errors disappear. And, if I
    use the Saxon parser to manually combine the xsl with
    the xml, the output is fine. If anyone knows that this is
    a confirmed bug by Oracle, or any other relevant info on
    this, please let me know.

    Hi,
    Your lucky, i've just finished a project that used the oracle parser extensively.
    I think the problem may stem from your xsl. Although your cdata tags may be well formed, in the xsl you might need to escape the text again, this may mean that the xsl needs to print a further cdata tag around the data. This is because the parser (well the version I was using) strips the cdata tags before the transformation happens.
    This is the probably the cause of the nasty html output.

  • Use of XML parser to read/parse xml stored in DB as CLOB

    Hello,
    I am new to XML technology. I have xml data stored in DB as
    CLOB. I need to parse this data to retrieve VALUE fro certain
    tags. I would greatly appreciate any help. Any examples that
    manipulate xml data stored in DB can be of extreme help.
    DesiDBA

    As you use Oracle 9.0.1. We suggest you use XMLType instead of
    CLOB to store your XML documents.
    You can use xmlType.extract() to get the elements within the
    XMLDocuments.
    Please refer to our Oracle9i XML Demo for examples.
    http://otn.oracle.com/tech/xml/xdk_sample/xdkdemo_faq.html
    For Instructions:
    http://ws5.olab.oracle.com/faqdemo/source/instruction.xsql?
    pagename=ins_xmltype
    Or you have to do the SQL query to get XML documents and parse
    each document and extract the content yourself using XML Parser
    for PL/SQL.
    Thanks.

  • Can we use javax.xml.parser API's from j2sdk kit?

    Hi All,
    I am new to this jaxp API's and my first doubt is Can I use the javax.xml.parser API which come bundled with j2sdk-1.4.1 or Should I install JWSDP-1.3 to get to javax.xml.parsers API?
    Thanks

    Hi,
    I am trying to read an xml file on Solaris.
    Can we not use doc.getElementById(String elementID) after deriving doc obejct ?
    Here is the code I am trying to use:
    try
    factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(false);
    File file = new File("/command/parse_doc.xml");
    doc = factory.newDocumentBuilder().parse(file); //upto here it works
    Element element = doc.getElementById("Topics");
    String name = element.getTagName(); //returns nothing but throws exception "null"
    }catch(Exception exe){
    out.println("Exception:" + exe.getMessage());}
    here I get no such element, and the try catch block exception also null.
    Is there something I am missing?
    DO I have to call doc.getDocumentElement() before I call get ElementBy ID() and if so why?
    Thanks.

Maybe you are looking for

  • How to get total number of nodes in a JTree?

    Hi, I am trying to get total number of nodes in a JTree, and cannot find a way to do it. The current getRowCount() method returns the number of rows that are currently being displayed. Is there a way to do this or I am missing something? thanks,

  • Sub-contracting Purchase order not functioning properly

    Hi     We are having issues with a PO 45007xxxxx. This PO is set-up as a subcontracting PO. In the component section I have a material 1232  that it is supposed to consume material 1232 when material 97 is received against this order. At the end of M

  • 4.1.2 stuck on processing

    Hey, I'm working with compressor 4.1.2 and final cut X.  These other details may not be relevant, but I am on a macbook pro with 8GB RAM, 2.66GHz intel core i7. Am working this project from an external drive, so things are a bit slower, but haven't h

  • File adapter..strange requirement..is it possible in XI?

    Hi Guys, I have searched sdn a lot..but have nt seen this kinda requirement before.. sender fie adapter files will be compare two files in a prticular folder  by say field1 and the differences between the two files is 'x'. If the delta > 30% of the t

  • Pause music in SWF file?

    When I play back SWF files from Newgrounds that have background music, and I pause the movie with Enter, the music pauses as well to keep in synch with the animation. Yet when I play back test movies in Flash, the music keeps playing even when I paus