[英]problem with using SAX XML Parser
I am using the SAX Parser for XML Parsing. 我正在使用SAX Parser进行XML解析。 The problem is for the following XML code:
问题在于以下XML代码:
<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: 持有XML解析信息的对象:
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;
}
}
You have to concatenate characters which the parser gives to you until it calls endElement
. 您必须串联解析器提供给您的字符,直到调用
endElement
为止。
Try removing currentElement = false;
尝试删除
currentElement = false;
from characters
handler, and 来自
characters
处理程序,以及
currentValue = currentValue + new String(ch, start, length);
Initialize currentValue
with an empty string or handle null
value in the expression above. 使用空字符串初始化
currentValue
或在上面的表达式中处理null
值。
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).
尝试将每个字符块打印在单独的行上,以进行调试(在if之前)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.