简体   繁体   中英

sax parser returns empty string from xml

i'm trying to parse xml with stax. after i'm going to this line in xml -

<temperature>38</temperature>

i am get a empty string value. Why this happens ? What can i do to solve this ? my xml :

<?xml version="1.0" encoding="UTF-8"?>
<flowers>
    <flower name="rose">
        <soil>podzolic</soil>   
        <visualParameters>
            <stemColor>Green</stemColor>
            <leafColor>Red</leafColor>
            <averageSize>50</averageSize>
        </visualParameters>
        <growingTips>
            <temperature>38</temperature>
            <watering>1200</watering>
            <value>photophilous</value>
        </growingTips>
    <multiplying>bySeeds</multiplying>
    <origin>Belarus</origin>
        <description>Classic Choice</description>
    </flower>
</flowers>

my code :

  List<Flower> getFlowerList() {
        return flowerList;
    }
    public void startElement(String namespaceURI, String localName, String qName, Attributes attrs) throws SAXException { 
        switch (qName) {
            case "flowers":
                flowerList = new ArrayList<Flower>();
                break;
            case "flower":
                flower = new Flower();
                flower.setName(attrs.getValue("name"));
                break;
            case "visualParameters":
                visual = new VisualParameters();
                break;
            case "GrowingTips":
                tips = new GrowingTips();
                break;
        }

    }
    public void endElement(String namespaceURI, String localName, String qName) throws SAXException { 
        switch (qName) {
            case "flower":
                flowerList.add(flower);
                break;
            case "soilType":
                soil = new SoilType();
                soil.setValue(SoilName.fromValue(content));
                break;
            case "origin":
                flower = new Flower();
                flower.setOrigin(content);
              break;
            case "averageSize":
                  visual.setAverageSize(Integer.valueOf(content));
              flower.setParameters(visual);
               break;
            case "leafColor":
                visual.setLeafColor(content);
                flower.setParameters(visual);
                break;
            case "temperature":
             //in this line content = ""
                tips.setTemperature(Integer.valueOf(content));

                break;
            case "stemColor":
                visual.setStemColor(content);
                flower.setParameters(visual);
                break;
            case "watering":
                tips.setWatering(Integer.valueOf(content));
                break;
            case "lightingType":
                tips.setValue(LightingName.fromValue(content));
                break;
            case "multiplying":
                multiplyingType = new MultiplyingType();
                multiplyingType.setValue(MultiplyingName.fromValue(content));
                break;
            case "description":
                flower.setDescription(content);
                break;
        }

    }
 public void characters(char[] ch, int start, int length) {
        content = String.copyValueOf(ch, start, length).trim();
    }

and the stacktrace:

java.lang.NullPointerException
    at tasks.five.parser.SAXHandler.endElement(SAXHandler.java:75)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(Unknown Source)
    at javax.xml.parsers.SAXParser.parse(Unknown Source)
    at tasks.five.parser.SAXParserClass.SAXParser(SAXParserClass.java:22)
    at tasks.five.command.SAXParserCommand.execute(SAXParserCommand.java:13)
    at tasks.five.command.CommandExecutor.execute(CommandExecutor.java:21)
    at tasks.five.runner.Main.main(Main.java:14)

Your case code in startElement() is searching for "GrowingTips" instead of "growingTips" , and case statements are case sensitive. So in the endElement() code, when you try to set the temperature, tips is null, and causes NullPointerException to be thrown.

Your characters() method is wrong:

content = String.copyValueOf(ch, start, length).trim();

Text data can be divided up into chunks any way the parser chooses, and delivered in multiple calls of characters() . You need to append to the value of content , not to overwrite it each time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM