1

I am using the SAX Parser for XML Parsing. The problem is for the following XML code:

<description>
Designer:Paul Smith Color:Plain Black Fabric/Composition:100% cotton        Weave/Pattern:pinpoint Sleeve:Long-sleeved Fit:Classic Front style:Placket front Back style:Side pleat back Collar:Classic/straight collar Button:Pearlescent front button Pocket:rounded chest pocket Hem:Rounded hem
</description>

I get this:

Designer:Paul Smith
Color:Plain Black 

The other parts are missing. The same thing happens for a few other lines. Can anyone kindly tell me whats the problem with my approach ?

My code is given below:

Parser code:

try {
        /** Handling XML */
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        /** Send URL to parse XML Tags */
        URL sourceUrl = new URL(
        "http://50.19.125.224/Demo/VeryGoodSex_and_the_City_S6E6.xml");

        /** Create handler to handle XML Tags ( extends DefaultHandler ) */
        MyXMLHandler myXMLHandler = new MyXMLHandler();
        xr.setContentHandler((ContentHandler) myXMLHandler);
        xr.parse(new InputSource(sourceUrl.openStream()));

    } catch (Exception e) {
        System.out.println("XML Pasing Excpetion = " + e);
    }

Object to hold XML parsed Info:

public class ParserObject {

String name=null;
String description=null;
String bitly=null; //single
String productLink=null;//single
String productPrice=null;//single
Vector<String> price=new Vector<String>();
}

Handler class:

public void endElement(String uri, String localName, String qName)
throws SAXException {


    currentElement = false;


    if (qName.equalsIgnoreCase("title"))
    {
        xmlDataObject[index].name=currentValue;
    }

    else if (qName.equalsIgnoreCase("artist"))
    {
        xmlDataObject[index].artist=currentValue;
    } 

}


public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {


    currentElement = true;

    if (qName.equalsIgnoreCase("allinfo"))
    {
        System.out.println("started");
    }

    else if (qName.equalsIgnoreCase("tags"))
    {
        insideTag=1;
    } 

}

public void characters(char[] ch, int start, int length)
throws SAXException {

    if (currentElement) {
        currentValue = new String(ch, start, length);
        currentElement = false;
    }

}
1

2 Answers 2

2

You have to concatenate characters which the parser gives to you until it calls endElement.

Try removing currentElement = false; from characters handler, and

currentValue = currentValue + new String(ch, start, length);

Initialize currentValue with an empty string or handle null value in the expression above.

Sign up to request clarification or add additional context in comments.

Comments

1

I think characters read some, but not all characters at the same time. Thus, you only get the first "chunk". Try printing each character chunk on a separate line, as debugging (before the if).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.