sax 解析器 returns 来自 xml 的空字符串
sax parser returns empty string from xml
我正在尝试用 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();
}
和堆栈跟踪:
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)
您在 startElement()
中的案例代码正在搜索 "GrowingTips"
而不是 "growingTips"
,并且案例语句区分大小写。因此在 endElement()
代码中,当您尝试设置温度时,tips
为 null,并导致抛出 NullPointerException
。
你的characters()
方法是错误的:
content = String.copyValueOf(ch, start, length).trim();
文本数据可以按解析器选择的任何方式分成块,并在 characters()
的多次调用中传送。您需要附加到 content
的值,而不是每次都覆盖它。
我正在尝试用 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();
}
和堆栈跟踪:
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)
您在 startElement()
中的案例代码正在搜索 "GrowingTips"
而不是 "growingTips"
,并且案例语句区分大小写。因此在 endElement()
代码中,当您尝试设置温度时,tips
为 null,并导致抛出 NullPointerException
。
你的characters()
方法是错误的:
content = String.copyValueOf(ch, start, length).trim();
文本数据可以按解析器选择的任何方式分成块,并在 characters()
的多次调用中传送。您需要附加到 content
的值,而不是每次都覆盖它。