Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

SaxParser problem. attributes.getValue() returns strange data

807605Jul 31 2007 — edited Jul 31 2007
Hello.

I'm using SaxParser to parse an xml-document.

After updating to java 1.6 I got weird results when calling attributes.getValue(someString);

Strangely this happens only when I have a really long string of data in an element.

Another weird thing is that the values I recieve when calling getValue() is the right number of characters, but they are the wrong characters. Accually the string I get seems to come from somewhere in the data of the element.

This is what the xml document looks like:
<world>
<picture name="Picture" positionX="57.272408" positionY="-0.52022195" positionZ="76.156906" 
rotationX="-0.0037210481" rotationY="0.69323385" rotationZ="0.72069275" rotationW="-0.0038684383" 
width="12.0" height="9.0" imageWidth="512" 
imageHeight="512" >e1fdffe1fdff...(about 1.5 Megabyte of hex data)...fe9fdffe9f</picture>
</world>
When calling attributes.getValue("positionX") for example, I don't get "57.272408" but instead something from the hexdata like "2b9401289" and it's always the same number of characters as the string I'm supposed to get, in this case 9.

If I decrease the size of the hex data significantly, this error doesn't occur.

Here's what my code looks like:
    public void load(URL url){
	this.clear();
	SAXParserFactory factory = SAXParserFactory.newInstance();
        	try {

            	// Parse the input
            	SAXParser saxParser = factory.newSAXParser();
            	saxParser.parse( url.toString(), this );

        	} catch (ParserConfigurationException ex) {
            	ex.printStackTrace();
        	} catch (IOException ex) {
            	ex.printStackTrace();
        	} catch (SAXException ex) {
            	ex.printStackTrace();
        	}
    }
    public void startElement(String namespaceURI,
                             String sName, // simple name (localName)
                             String qName, // qualified name
                             Attributes attrs)
    throws SAXException
    {   
 
        this.attributes = attrs;
        String eName = sName; // element name
        if ("".equals(eName)) eName = qName; // namespaceAware = false
        this.elementName = eName;
        if(elementName.equals("picture")){
            this.stringBuffer = new StringBuffer();
        }
 
    }


    public void characters(char buf[], int offset, int len)
    throws SAXException
    {
        
        if(elementName.equals("picture")){
            String string = new String(buf,offset, len);
            stringBuffer.append(string);
        }
 
    }

    public void endElement(String namespaceURI,
                           String sName, // simple name
                           String qName  // qualified name
                          )
    throws SAXException
    {
        String eName = sName; // element name
        if ("".equals(eName)) eName = qName; // namespaceAware = false
        
        if(eName.equals("picture")){
            
            System.out.println(this.attributes.getValue("positionX"));
            
        }
      
    }
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 28 2007
Added on Jul 31 2007
1 comment
209 views