sax解析器从xml返回空字符串

时间:2017-01-24 15:46:14

标签: java xml sax saxparser

我正在尝试使用stax解析xml。 在我要去xml的这一行 -

之后
<temperature>38</temperature>

我得到一个空字符串值。为什么会这样?我该怎么做才能解决这个问题?  我的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>

我的代码:

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

和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)

2 个答案:

答案 0 :(得分:1)

[[ url | {F4711} ]] 中的案例代码正在搜索startElement()而不是"GrowingTips",并且案例陈述区分大小写。因此,在"growingTips"代码中,当您尝试设置温度时,endElement()为空,并导致tips被抛出。

答案 1 :(得分:1)

您的characters()方法错误:

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

文本数据可以按照解析器选择的方式划分为多个块,并在多个characters()调用中传递。您需要附加content的值,而不是每次都覆盖它。