SAX xml httprequest

Hi all;
i ve deleloped a sax xml reader as well with no problem..i can read my local files...
But now what i want to do is to read the indirect addressed xml files on my remote server.For example my xml is on that link:http://mysite.com/bring.jsp?mode=viewxml NOT a direct link like http://mysite.com/bring.xml
so how can i make a http request and load this file.?please show me a way.
thanks a lot
Burak

You use the same method for both URLs. I have no idea about what you have tried or what you know so I will suggest the tutorial first:
http://java.sun.com/docs/books/tutorial/networking/

Similar Messages

  • 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.

  • SAX XML Parser problem

    Hi,
    I have a standard .xml file and a file called TemplateContentHandler that parses the info in the xml doc but there is an error in it. See below:
    TemplateContentHandler.java
    if ( strElementName.equals( TemplateConstants.RNC_Module_LDN_TU1 ) ) {
                timingUnit1TagExistInDefaultTemplateFile = true;
                System.err.println("timingUnit1TagExistInDefaultTemplateFile for TU1 == "+timingUnit1TagExistInDefaultTemplateFile);
                System.err.println("strValue for TU1 == "+strValue);
                tTemplate.setRNC_Module_LDN_TU1( strValue );
                Trace.exit();
                return;
            if ( strElementName.equals( TemplateConstants.RNC_Module_LDN_TU2 ) ) {
                timingUnit2TagExistInDefaultTemplateFile = true;
                System.err.println("timingUnit2TagExistInDefaultTemplateFile for TU2 == "+timingUnit2TagExistInDefaultTemplateFile);
                System.err.println("strValue for TU2 == "+strValue);
                tTemplate.setRNC_Module_LDN_TU2( strValue );       
                Trace.exit();
                return;
            }DefaultTemplate
    <RNC_Module_LDN_TU2>ManagedElement=1,Equipment=1,Subrack=MS,Slot=5,PlugInUnit=1</RNC_Module_LDN_TU2>
    <RNC_Module_LDN_TU1>ManagedElement=1,Equipment=1,Subrack=MS,Slot=4,PlugInUnit=1</RNC_Module_LDN_TU1>
    OutPut
    root@atrcus13> timingUnit2TagExistInDefaultTemplateFile for TU2 == true
    strValue for TU2 == ManagedElement=1,Equipment=1,Subrack=MS,Slot=5,PlugInUnit=1
    timingUnit1TagExistInDefaultTemplateFile for TU1 == true
    strValue for TU1 == ManagedElement=1,Equipment=1,Subrack=MS,SlottimingUnit1TagExistInDefaultTemplateFile for TU1 == true
    strValue for TU1 == =4,PlugInUnit=1
    as you can see it parses the Tag <RNC_Module_LDN_TU1> twice and splits it in2 pieces. does anyone know why this is? Also I added a dirty hack whcih works:
    private int timimngUnit1Tagcounter = 0;
        private int timimngUnit2Tagcounter = 0;
    if( timimngUnit1Tagcounter == 0) {
                    tempTU1 = strValue;
                    timimngUnit1Tagcounter++;
                    Trace.message("FIRST TIME STRVALUE === "+strValue);
                    tTemplate.setRNC_Module_LDN_TU1( strValue );
                else {
                    Trace.message("SECOND TIME STRVALUE === " + tempTU1+strValue);
                     tTemplate.setRNC_Module_LDN_TU1( tempTU1+strValue );
                     timimngUnit1Tagcounter = 0;
                }but now the parser is screwing up other tags in the file; anyone have any ideas?
    Thanks,
    Vinnie

    In which method is the code you've posted called?
    Given your results, I'd suspect that it's in characters, and strValue is a string created from the character data passed in.
    in that case, read
    http://java.sun.com/j2se/1.4.2/docs/api/org/xml/sax/ContentHandler.html#characters(char[],%20int,%20int)
    in particular
    The Parser will call this method to report each chunk of character data. SAX parsers may return all contiguous character data in a single chunk, or they may split it into several chunks; however, all of the characters in any single event must come from the same external entity so that the Locator provides useful information.
    The accepted pattern of use where you want to process all character data between tags in one go is to use a StringBuffer to acculmulate the characters, then process them in the following endElement or beginElement method.
    Pete

  • SAX XML Parsing

    I am very new to JAVA, but been doing JavaScript for many years. I am working in an Eclipse based editor. I have the task of reading an XML file, parsing it, using the tags as table and fields, and the values as parameters. I was successfully able to parse the XML easily with SAX, but I am having a hell of a time controling my values.
    Can someone point me in the right direction on how to distinguish the tag values from the data values returned from XML? I basically need to write select statements based on these XLM files, retrieve the data from SQL, then build the output in an XML structure.
    One more thing; this has to be dynamic. It needs to be able to accept random XML structures because I'll never know what is going to be imported in.
    Thanks for any help.
    -Motley Drew

    Sorry, I've never done much with XML, so i don't know
    the proper terminology. Here is a simple sample of
    what I mean.
    <customer>
    <name>MonkeyBoy Inc</name>
    <state>AZ</state>
    </customer>Okay. In that example, "customer", "name", and "state" are element names. Your example shows a "customer" element. It has for children a "name" element and a "statement" element (plus some whitespace text elements). The "name" element and the "state" element both have a single child, a text node in both cases.
    If you're using a SAX parser then the startElement() method gives you an element name. The characters() method gives you characters from a text node. Beware of this because it will give you the whitespace that you want to ignore, and it can give you a text node in pieces (more than one call).
    The term "tag" does exist in XML but it is restricted to talking about begin tags (the <customer> bit at the start of the element) and end tags (the </customer> bit at the end of the element).

  • SAX XML NullPointerException in character() method

    I'm having issues when parsing my xml file. Everything works fine unless I am trying to push values into my parameters Vector.
    class SAXLoader extends DefaultHandler {
        private static String className, //name of the class for module in xml
                methodName,              //name of the method for the class
                path;                    //path to jar. should be in URI format
        private Vector params;      //parameters to the method
        private boolean isClassName,     //true if element begins className tag
                isMethodName,            //true if element begins methodName tag
                isParameter,             //true if element begins parameter tag
                isPath;                  //true if element begins path tag
        //=======================init()================================
        //opens XML file specified and parses it for className,
        //the methodName,and the path to the JAR
        //This should be called before doing any of the getter methods
        //=============================================================
        public void init() {
            try {
                XMLReader xmlreader = XMLReaderFactory.createXMLReader();
                SAXLoader handler = new SAXLoader();
                xmlreader.setContentHandler(handler);
                xmlreader.setErrorHandler(handler);
                try {
                    FileReader r = new FileReader("myclass.xml");
                    xmlreader.parse(new InputSource(r));
                } catch (FileNotFoundException e) {
                    System.out.println("File Not Found!");
                    e.printStackTrace();
                } catch (NullPointerException e) {
                    System.out.println("Parse Error!");
                    e.printStackTrace();
            } catch (Exception e) {
                System.out.println("XML Reader Error!");
                e.printStackTrace();
    public void characters(char ch[], int start, int length) {
            String temp = "";
            for (int i = start; i < start + length; i++) {
                switch (ch) {
    case '\\':
    // System.out.print("\\\\");
    break;
    case '"':
    // System.out.print("\\\"");
    break;
    case '\n':
    // System.out.print("\\n");
    break;
    case '\r':
    // System.out.print("\\r");
    break;
    case '\t':
    // System.out.print("\\t");
    break;
    default:
    temp += ch[i];
    break;
    if (isClassName) {
    className = temp;
    } else if (isMethodName) {
    methodName = temp;
    } else if (isPath) {
    path = temp;
    } else if (isParameter) {
    System.out.println("zomg!: " + temp);
    params.add(temp);
    } //end of characters(...
    Right there at params.add(temp);
    I'm using this DefaultHandler based class to parse an XML file for information regarding a user's own class [its classname, its methodname, and the methods parameters].
    Any clues?
    Message was edited by:
    nick.mancuso
    Message was edited by:
    nick.mancuso

    You don't actually allocate the params Vector, at least not on the code you've posted. You want something like:
    private Vector params = new Vector();or better yet:
    private List params = new LinkedList();perhaps.
    But even better yet...I suspect that having a handful of global temp variables like that isn't going to work. In my experience, when using SAX you pretty much have to create a stack to hold the current place in the XML tree. That's how you traverse trees, with stacks. Then instead of setting isMethodName, isParameter, etc. (which I presume you do in open/close tag handlers) you define an enumeration of values like "class", "method", "parameter", and then have a stack of those values, or something.

  • 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 ;)

  • JAVA-SAX-Xml how to get simple tag element names to a list

    i need to get the simple tag names(here it is name,price,aname,city) in to a list i have heard about SAX parser event driven it walks throgh the xml step by step when a new tag occurs it will parse etc....
    but i am unable to get the only sampletag names into a Arraylist
    i have heard about a method getElementBytagName()
    i dont want to write a code for particular xml because later on i have to retrive same simple tags from another xml say employee.xml
    please provide a generic code which will retrive the simple tag names and stores into a Arraylist
    the real xml i want to store only simple elements into array list
    <ProvisionViewingCardprovisionViewingCar>
    <orderId>1</orderId>
    <numberOfCards>2</numberOfCards>
    <customerSite>
    <customerSiteSystemId>3</customerSiteSystemId>
    <customerSiteID>4</customerSiteID>
    <customerSiteState>5</customerSiteState>
    <parentCustomerSiteID>6</parentCustomerSiteID>
    <businessName>7</businessName>
    <migratedCustomerID>8</migratedCustomerID>
    <businessContactInformation>
    <contactID>9</contactID>
    <emailInfo>
         <additionalEmailAddress>10</additionalEmailAddress>
         <emailAddress>11</emailAddress>
    </emailInfo>
    <communicationPreference>12</communicationPreference>
    <mobileNumber>13</mobileNumber>
    <faxNumber>14</faxNumber>
    <contactExceptions>15</contactExceptions>
    <telephoneNumber>16</telephoneNumber>
    </businessContactInformation>
    <address>
    <addressID>17</addressID>
    <addressLine1>18</addressLine1>
    <addressLine2>19</addressLine2>
    <addressLine3>20</addressLine3>
    <town>21</town>
    <county>22</county>
    <postCode>22_1</postCode>
    <country>23</country>
    </address>
    <person>
    <personID>24</personID>
    <title>25</title>
    <firstName>26</firstName>
    <middleName>27</middleName>
    <dataProtectionPreference>28</dataProtectionPreference>
    <surname>29</surname>
    <dateOfBirth>30</dateOfBirth>
    <position>31</position>
    <!--1 or more repetitions:-->
    <authority>32</authority>
    <address>
    <addressID>33</addressID>
    <addressLine1>34</addressLine1>
    <addressLine2>35</addressLine2>
    <addressLine3>36</addressLine3>
    <town>37</town>
    <county>38</county>
    <postCode>39</postCode>
    <country>40</country>
    </address>
    Regards/
    Suman.

    i need to get the simple tag names(here it is name,price,aname,city) in to a list i have heard about SAX parser event driven it walks throgh the xml step by step when a new tag occurs it will parse etc....
    but i am unable to get the only sampletag names into a Arraylist
    i have heard about a method getElementBytagName()
    i dont want to write a code for particular xml because later on i have to retrive same simple tags from another xml say employee.xml
    please provide a generic code which will retrive the simple tag names and stores into a Arraylist
    the real xml i want to store only simple elements into array list
    <ProvisionViewingCardprovisionViewingCar>
    <orderId>1</orderId>
    <numberOfCards>2</numberOfCards>
    <customerSite>
    <customerSiteSystemId>3</customerSiteSystemId>
    <customerSiteID>4</customerSiteID>
    <customerSiteState>5</customerSiteState>
    <parentCustomerSiteID>6</parentCustomerSiteID>
    <businessName>7</businessName>
    <migratedCustomerID>8</migratedCustomerID>
    <businessContactInformation>
    <contactID>9</contactID>
    <emailInfo>
         <additionalEmailAddress>10</additionalEmailAddress>
         <emailAddress>11</emailAddress>
    </emailInfo>
    <communicationPreference>12</communicationPreference>
    <mobileNumber>13</mobileNumber>
    <faxNumber>14</faxNumber>
    <contactExceptions>15</contactExceptions>
    <telephoneNumber>16</telephoneNumber>
    </businessContactInformation>
    <address>
    <addressID>17</addressID>
    <addressLine1>18</addressLine1>
    <addressLine2>19</addressLine2>
    <addressLine3>20</addressLine3>
    <town>21</town>
    <county>22</county>
    <postCode>22_1</postCode>
    <country>23</country>
    </address>
    <person>
    <personID>24</personID>
    <title>25</title>
    <firstName>26</firstName>
    <middleName>27</middleName>
    <dataProtectionPreference>28</dataProtectionPreference>
    <surname>29</surname>
    <dateOfBirth>30</dateOfBirth>
    <position>31</position>
    <!--1 or more repetitions:-->
    <authority>32</authority>
    <address>
    <addressID>33</addressID>
    <addressLine1>34</addressLine1>
    <addressLine2>35</addressLine2>
    <addressLine3>36</addressLine3>
    <town>37</town>
    <county>38</county>
    <postCode>39</postCode>
    <country>40</country>
    </address>
    Regards/
    Suman.

  • 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);
        }

  • SAX XML format

    Hi all,
    I've got the following unsual XML format
    <doc>hello <name attr="1" attr2="2">Tom</name>. i am <name>Sue</name>. cheers</doc>
    How do I write a SAX parser to read and parse this file and the result should be (in a vector or collection or whatever)
    doc:hello .i am .cheers
    name:Tom
    name:Sue
    (basically node names and their values)
    Thanks & regards,
    Tim

    Don't waste your time writing an XML parser. Save yourself several months and use one of the XML parsers that are already available. For the particular problem, I would not use Java at all. I would use XSLT, in which it is one line of code to do what you want.

  • Sax xml format problem

    I must parse an xml file(quite large) and there is an "<" character inside of an attribute name (instead of "& lt").
    Of course it appears an SAXParseException and the program stops; the attribute name is not important for me (for example I could ignore < ), but I want that the program to continue to parse.
    Which is the best way to solve this problem?
    thanks, liviu

    XML parsers will not parse documents unless they are well-formed XML.
    Your document is not well-formed XML.
    Therefore it follows that you must fix the document. Contact the person who sent it to you and tell them not to send you garbage any more.

  • Is there a way to get the actual XML string when using the JAXP SAX Parser?

    Hi All,
    I am using a JAXP SAX xml parser and am looking for a way to get the actual line of xml that is being parsed from within a content handler.
    Here's my scenario. Consider the following example xml document.
    <formCollection>
       <form name="myForm">
          <text/>
          <selection/>
       </form>
       <form name="anotherForm">
          <text/>
       </form>
    </formCollection>My hope is to validate the entire document and then insert an xml string containing each "form" element (and its sub-elements) into a map for later use. My thought was to create a String as each "form" element is being parsed (begining with the form's startElement event and concatenating until the form's endElement event is reached) and then inserting this string into the map. Is there a way to get the actual line of xml that is being parsed, rather than just the element name and attribute values?

    DrClap wrote:
    YouRang wrote:
    2. The first handler would validate the entire XML document, extract the "type" attribute from each <form> element, and place each <form> element and its sub-elements into the map of Strings, using the "type" attribute as the key. The second handler would take a <form> element and parse it into a Form object for the display. This option was the impetus for my question because it relies on the first handler being able to access the entire <form> element XML String and write it to a map.I don't see why you need the raw data from the XML document here. You should already be abstracting your data into Java classes in the first handler, instead of making the second handler do the parsing all over again.Correct, I am not referring to XForms. In this case, it happens that I am using the XML to generate an SWT ScrolledForm object and, thus, the XML element name happens to be named "form." However, the concept/design problem could apply to any type of object and the XML element could be appropriately renamed.
    My experience with XSLT is limited and I haven't done anything with it for several years. With that said, it seems that it is primarily used for generating web content, which wouldn't apply in this case because this is for a client-server application. I could be off base on this one -- if XSLT applies to much broader translations and would be more appropriate for generating Java objects than my current methodology, I could certainly look into it further.
    I apologize that option two didn't make more sense; it is difficult to explain. Yes, optimally the data should be abstracted into Java classes in the first handler. This is really an elaboration that I failed to specify when explaining option one. The problem is that the user can choose to create any number of "forms" of any type. For instance, let's say that from the File -> New menu there are options for seven different types of forms and each form is used to send completely different data. The user can select form1, select form1 again, select form4, and select form7 (or any other combination) and bring up tabs that display the different forms. The user can then enter data and submit each form separately. When the user selects File -> New -> FormX, a SWT ScrolledForm object that corresponds with FormX must be given to the display. Because SWT ScrolledForm objects do not allow a deep copy, I cannot simply read the XML <form> elements at initialization, parse them into ScrolledForm objects, and pass deep copies of the ScrolledForm objects to the display each time the user clicks File -> New -> FormX. The only simple way I see of getting a new copy of a ScrolledForm object is to reparse the appropriate XML <form> element in order to create one each time the user selects File -> New -> FormX. As such, one handler would need to read the entire XML document and parse the <form> elements into a map (or some other data structure) and another handler would need to parse individual <form> elements into SWT ScrolledForm objects. The first handler would be called at initialization and the second handler would be called each time a user clicked on File -> New -> FormX. Once again, this isn't exactly my favorite implementation... but seems the simplest given that there is no way to do a deep copy of an SWT ScrolledForm object. Hopefully that makes a little more sense. No worries if it doesn't -- I just figured I'd throw this out there and see if anyone had a better idea.

  • Need suggestion about a fast XML parser

    I have a program which needs to parse lots of XML files of sizes varing from few MBs to hundreds of MBs. It needs to parse the file in one-pass, and for which the SAX approach is best suited. I initially tried coding this program using the SAX parser provided by Java library, but it takes way-way too much time to parse the files. I then googled out Piccolo XML Parser, but it throws ArrayIndexOutOfBound exception. Furthermore, sometimes XML files could terminated prematurely.
    Can you suggest any SAX XML parser for this job?

    I am pasting here a trimmed version of my code..... sorry this is not the simplified and complete in itself version as the norm is.
    public Vector<String> search(String dumpfile, String srfor, int type, long size, boolean showTTH, boolean phpSerialize, String hubname) {
         Vector<String> results = new Vector<String>();
         try {
             SAXParserFactory factory = SAXParserFactory.newInstance();
             SAXParser parser = factory.newSAXParser();
             BufferedInputStream ubin = new BufferedInputStream(new FileInputStream(dumpfile));
             ubin.read(new byte[2]); //To discard the starting BZ flag.
             CBZip2InputStream bin = new CBZip2InputStream(ubin);
             String line = null;
             int c=0;
             line = "";
             while((c=bin.read())!='\n' && c!=-1) line = line + (char) c;
             String dnt = line;
             if (c==-1){// (line == null){
              dnt="";
              return null;
             line = "";
             while((c=bin.read())!='\n' && c!=-1) line = line + (char) c;
             if(c==-1)// (line == null)
              return null;
             if (!phpSerialize)
              results.add("Dump's Date and Time stamp: " + dnt + "\n==========================\n"+"hubname: " + line);
             else
              results.add("$" + dnt +"\n"+"|" + line);
             if(hubname!=null && !line.trim().toLowerCase().contains(hubname.toLowerCase().subSequence(0, hubname.length()))){
              results.add("No hits.");
              return results;
             while((c=bin.read())!='\n' && c!=-1);
             FilelistHandler handler = new FilelistHandler(srfor,type,size,showTTH,phpSerialize,hubname,results);
             try{
              parser.parse(bin, handler);
             }catch(org.xml.sax.SAXParseException saxe){
              saxe.printStackTrace();
             if(results.size()==1){
              results.add("No hits.");
         } catch (FileNotFoundException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         } catch (ParserConfigurationException e) {
             e.printStackTrace();
         } catch (SAXParseException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
             System.err.println("Line: "+e.getLineNumber()+"; Col:"+e.getColumnNumber());
         } catch (SAXException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         return results;
    private class FilelistHandler extends DefaultHandler {
         static final int USER = 0;
         static final int FILE = 1;
         static final int DIR = 2;
         static final int ANY = 3;
         static final int UNKNOWN = 4;
         private Vector<String> results;
         Vector<String> dirs = new Vector<String>();
         private String srfor;
         private int type;
         private int level = -1;
         private long size;
         private boolean showTTH;
         private boolean phpSerialize;
         private String hubname;
         private String currentUser;
         private String currentIP;
         public FilelistHandler(String Srfor, int Type, long Size, boolean ShowTTH, boolean PhpSerialize, String Hubname, Vector<String> res) {
             srfor = Srfor.trim().toLowerCase();
             type = Type;
             size = Size;
             showTTH = ShowTTH;
             phpSerialize = PhpSerialize;
             hubname = Hubname;
             results = res;
         public void startElement(String uri, String lname, String qname, Attributes attrs) throws SAXException {
             String result = null;
             long currsize = 0L;
             String TTH = "";
             String value = "";
             int currType = UNKNOWN;
             if (qname.equalsIgnoreCase("Directory")) {
              value = attrs.getValue("Name");
              dirs.add(attrs.getValue("Name"));
              currType = DIR;
             } else if (qname.equalsIgnoreCase("user")) {
              currentUser = attrs.getValue("username");
              currentIP = attrs.getValue("ip");
              currType = USER;
             } else if (qname.equalsIgnoreCase("File")) {
              value = attrs.getValue("Name");
              currsize = Long.parseLong(attrs.getValue("Size"));
              TTH = attrs.getValue("TTH");
              currType = FILE;
             if (currType == FILE || currType == DIR) {
              // Searching.
              boolean found = false;
              result = currentUser + ":" + currentIP + ":" + (currType == FILE && showTTH ? TTH + ":" : "") + getPwd(dirs);
              if (result.trim().toLowerCase().contains(srfor.subSequence(0, srfor.length())) || currType == FILE && TTH.equalsIgnoreCase(srfor)) {
                  found = true;
              if (type != ANY && currType != type)
                  found = false;
              if (size >= 0 && currType == FILE)
                  if ((size == 0 && currsize != size) || (size != 0 && (((double) (Math.abs(currsize - size))) / size) > 0.1))
                   found = false;
              if (found) {
                  if (!phpSerialize) {
                   if (currType == FILE)
                       result = result + "/" + value;
                  } else {
                   int index = 0;
                   result = serializeEntity(index++, currType == FILE ? "f" : "d");
                   result = result + serializeEntity(index++, currentUser);
                   result = result + serializeEntity(index++, currentIP);
                   if (currType == FILE && showTTH)
                       result = result + serializeEntity(index++, TTH);
                   result = result + serializeEntity(index++, getPwd(dirs));
                   if (currType == FILE)
                       result = result + serializeEntity(index++, value);
                   result = "a:" + index + ":{" + result + "}";
                  results.add(result); // ADDING THE RESULT.
         public void endElement(String uri, String lname, String qname) throws SAXException {
             if (qname.equalsIgnoreCase("Directory")) {
              dirs.remove(dirs.size() - 1);
         private String getPwd(Vector<String> dirs) {
             String pwd = "";
             for (String dir : dirs) {
              pwd = pwd + "/" + dir;
             return pwd;
         private String serializeEntity(int index, String s) {
             return "i:" + index + ";s:" + s.length() + ":\"" + s + "\";";
         public Vector<String> getParsedData() {
             return results;
        }

  • Attribute parsing using the Oracle SAX Parser

    I am trying to parse attributes like <Phone Type="N"> using the SAX xml parser from oracle in the startElement method and am getting a null pointer exception
    public void startElement(String sName_, AttributeList attrs_) {
    if sName_.equals("Phone")
    String phoneType = attrs_.getValue("Type")
    Any ideas ??
    null

    Using 2.0.2.9, the following works for me:
    import oracle.xml.parser.v2.*;
    public class SAXParserExample {
    public static void main(String[] a) throws Throwable {
    SAXParser s = new SAXParser();
    Handler h = new Handler();
    s.setDocumentHandler(h);
    s.parse(new java.io.StringReader("<Phone Type='N'/>"));
    import org.xml.sax.*;
    public class Handler extends HandlerBase {
    public void startElement(String sName_, AttributeList attrs_) {
    if (sName_.equals("Phone")) {
    String phoneType = attrs_.getValue("Type");
    System.out.println(phoneType);
    }What's different about your case?

  • Writing XML files

    Hello all
    There's a vast amount of information on the java.sun.com site about parsing XML files:
    * creating DOM trees in memory from an XML file
    * setting up event handlers to be called when an XML file is parsed
    * transforming XML files using XSL.
    However, I can find very little information anywhere about writing XML files in the first place! I'm already aware of two ways to do this:
    (1) create a DOM in memory and write this to a file. If the data is large, though, this isn't feasible.
    (2) just output the XML elements directly to some file as text. This doesn't take account of the encoding, or special characters, and the document is not necessarily then syntactically valid.
    Is there some API which I haven't yet come across that is designed specifically for writing XML data to files? If it's built in to Java 2, then goodness knows how I've missed it all this time; if it's third-party, I'd be grateful for any links.
    Thanks for your help
    Rich Fearn

    I know pastes can be horrible, but this is a working example which I find useful: SAX XML output ;)
    import java.io.*;
    // SAX classes.
    import org.xml.sax.*;
    import org.xml.sax.helpers.*;
    //JAXP 1.1
    import javax.xml.parsers.*;
    import javax.xml.transform.*;
    import javax.xml.transform.stream.*;
    import javax.xml.transform.sax.*;
                    //AlphabetXMLOut(file, alphabets);
    class AlphabetXMLOut {
            public void run(){
                    try{
                            FileOutputStream fos = new FileOutputStream("output.xml");
                            PrintWriter out = new PrintWriter(fos);
                            StreamResult streamResult = new StreamResult(out);
                            // PrintWriter from a Servlet
                            //PrintWriter out = response.getWriter();
                            //StreamResult streamResult = new StreamResult(out);
                            SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
                            // SAX2.0 ContentHandler.
                            TransformerHandler hd = tf.newTransformerHandler();
                            Transformer serializer = hd.getTransformer();
                            serializer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1");
                            serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,"users.dtd");
                            serializer.setOutputProperty(OutputKeys.INDENT,"yes");
                            hd.setResult(streamResult);
                            hd.startDocument();
                            AttributesImpl atts = new AttributesImpl();
                            // USERS tag.
                            hd.startElement("","","USERS",atts);
                            // USER tags.
                            String[] id = {"PWD122","MX787","A4Q45"};
                            String[] type = {"customer","manager","employee"};
                            String[] desc = {"Tim@Home","Jack&Moud","John D'oe"};
                            for (int i=0;i<id.length;i++){
                                    atts.clear();
                                    atts.addAttribute("","","ID","CDATA",id);
    atts.addAttribute("","","TYPE","CDATA",type[i]);
    hd.startElement("","","USER",atts);
    hd.characters(desc[i].toCharArray(),0,desc[i].length());
    hd.endElement("","","USER");
    hd.endElement("","","USERS");
    hd.endDocument();
    fos.close();
    }catch (IOException e){
    System.err.println ("Unable to write to file");
    System.exit(-1);
    }catch (TransformerConfigurationException tce){
    System.err.println("Error in: TransformerConfigurationException");
    }catch (SAXException spe) {
    // Error generated by the parser
    System.err.println("Error in: (SAXException");
    // Use the contained exception, if any
    Exception x = spe;
    if (spe.getException() != null)
    x = spe.getException();
    x.printStackTrace();
    -Hope this helps

  • SAX Parser Gotcha

    For what its worth, the Java SAX XML had/has a killer gotcha that one must compensate for or it does not work.
    Essentially, a SAX parser has five routines one has to program:
    startDocument
    startElement
    characters
    endElement
    endDocument
    The characters routine is where the contents of a tag appear.
    Suppose the underlying disk read routine does a read of the
    input stream that does not terminate at a tag; that has to happen
    occasionally. Then all the characters routine will see is part
    of a tag's contents. The next read of the input stream will
    cause characters to be called again with the second half of
    the tag's contents in the buffer. The application has to be
    smart enough to append the second half of the tag's contents
    to the first half, or there will be an error. My solution was
    to initialize a string in startElement, always append to it in
    characters, and call it the contents of the tag in endElement.
    This seems to work for me.
    It took weeks to find and fix this error. And as far as I know
    there is no defense against it, other than the one I outined above.
    I have read several books on XML and have never seen this problem
    described.
    Has anyone else ever seen this problem and have a better soln
    for it?
    Charles Elliott

    From the API documentation for the characters() method of org.xml.sax.ContentHandler:
    The Parser will call this method to report each chunk of character data. SAX parsers may return all contiguous character data in a single chunk, or they may split it into several chunks; however, all of the characters in any single event must come from the same external entity so that the Locator provides useful information.You ask if anyone else has ever seen the problem? Actually it's an FAQ in this forum. Appears every couple of weeks. You've found the correct way to handle it. (Although using a StringBuilder instead of a String to accumulate the data might be a tiny bit better.)

Maybe you are looking for

  • I want to sent to values from a form to the sevlet

    Hello I have a html file with some like that: <form action="htpp://myservlet" method="post"> <label>Select your favorites interests</label> <input type="checkbox" name="interests" value="Economy" /> <input type="checkbox" name="interests" value="Spor

  • How to use Filechooser to look at a network

    The Filechooser class in Swing does not seem to let me look at anything except local drives on my machine. Is it capable of viewing other machines on a network? Or is it limited to the local machine it is being implemented on?

  • I just started using Time Machine, and it's driving me crazy.

    It's just constantly backing up. It never stops, even when nothing has changed on my computer, it finds another 3gbs. Really? Why? I expected Time Machine to work like Dropbox: if I add a file or change a file, it updates. But it really doesn't, and

  • 10.7.4 will not print on HP 4250dtn

    new Macbook pro with 10.7.4 won't print on my HP 4250dtn

  • Updating Mac OS X

    I have 10.4 something, and suddenly, downloads won't open and say I need 10.5. But when I download it and try to open it, it gives me the same message. I need 10.5 to open this package. What the frig do I do to update my OS X?!