[英]sax parser returns empty string from xml
i'm trying to parse xml with stax. 我正在尝试使用stax解析xml。 after i'm going to this line in xml -
在我转到xml中的这一行之后-
<temperature>38</temperature>
i am get a empty string value. 我得到一个空字符串值。 Why this happens ?
为什么会这样呢? What can i do to solve this ?
我该怎么解决? my xml :
我的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. 您在
startElement()
案例代码正在搜索"GrowingTips"
而不是"growingTips"
,并且case语句区分大小写。 So in the endElement()
code, when you try to set the temperature, tips
is null, and causes NullPointerException
to be thrown. 因此,在
endElement()
代码中,当您尝试设置温度时, tips
为null,并导致引发NullPointerException
。
Your characters()
method is wrong: 您的
characters()
方法错误:
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()
. 可以通过解析器选择的任何方式将文本数据划分为多个块,并通过
characters()
多次调用来传递文本数据。 You need to append to the value of content
, not to overwrite it each time. 您需要附加到
content
的值,而不是每次都覆盖它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.