这是我在互联网上的网址上的xml内容:
<skus>
<item>
<id>r_1</id>
<size>10</size>
</item>
<item>
<id>c_1</id>
<size>10</size>
</item>
<item>
<id>d_1</id>
<size>10</size>
</item>
<item>
<id>e_1</id>
<size>10</size>
</item>
<item>
<id>f_1</id>
<size>10</size>
</item>
</skus>
这是SAXXMLParser类:
public class SAXXMLParser {
public static List<XMLSetAdd> parse(InputStream is) {
List<XMLSetAdd> setAdds = null;
try {
// create a XMLReader from SAXParser
XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser()
.getXMLReader();
// create a SAXXMLHandler
SAXXMLHANDLER saxHandler = new SAXXMLHANDLER();
// store handler in XMLReader
xmlReader.setContentHandler(saxHandler);
// the process starts
xmlReader.parse(new InputSource(is));
// getting the list`
setAdds = saxHandler.getIds();
} catch (Exception ex) {
Log.d("XML", "SAXXMLParser: parse() failed");
ex.printStackTrace();
}
// return The list
return setAdds;
}
}
这是我的XMLSetAdd类:
public class XMLSetAdd {
public String getId() {
return Id;
}
public void setId(String Id) {
this.Id = Id;
}
public String getSize(){
return Size;
}
public void setSize(String Size){
this.Size = Size;
}
private String Id;
private String Size;
}
这是我的SAXXMLHANDLER:
public class SAXXMLHANDLER extends DefaultHandler {
private List<XMLSetAdd> setAdds;
private String tempVal;
// to maintain context
private XMLSetAdd setAdd;
public SAXXMLHANDLER() {
setAdds = new ArrayList<XMLSetAdd>();
}
public List<XMLSetAdd> getIds() {
return setAdds;
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
tempVal = "";
if (qName.equals("skus")){
}else if (qName.equals("item")){
setAdd = new XMLSetAdd();
}
}
public void characters(char[] ch, int start, int length)
throws SAXException {
tempVal = new String(ch, start, length);
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equals("item")) {
setAdds.add(setAdd);
} else if (qName.equals("id")) {
setAdd.setId(tempVal);
} else if (qName.equals("size")){
setAdd.setSize(tempVal);
}
}
}
最后在使用方面:
我有一个异步任务,目的是读取xml内容并将其填入列表中。运行此代码时得到的结果是“ONE ROW”,即r_1。而它应该返回5个结果。 (我也没有收到尺寸值)
我无法弄清楚我的代码的哪一部分是错误的!
答案 0 :(得分:0)
这就是你如何做到的
package com.teste;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
public class SAXXMLParser {
public static List<XMLSetAdd> parse(InputStream is) {
List<XMLSetAdd> setAdds = null;
try {
// create a XMLReader from SAXParser
XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser()
.getXMLReader();
// create a SAXXMLHandler
SAXXMLHANDLER saxHandler = new SAXXMLHANDLER();
// store handler in XMLReader
xmlReader.setContentHandler(saxHandler);
// the process starts
xmlReader.parse(new InputSource(is));
// getting the list`
setAdds = saxHandler.getIds();
System.out.println(setAdds.size());
for(int i=0;i<setAdds.size();i++)
{
System.out.println(setAdds.get(i).getId());
System.out.println(setAdds.get(i).getSize());
}
} catch (Exception ex) {
System.out.println("XML SAXXMLParser: parse() failed");
ex.printStackTrace();
}
// return The list
return setAdds;
}
public static void main(String[] args) throws FileNotFoundException {
SAXXMLParser parser = new SAXXMLParser();
InputStream inputStream = new FileInputStream(
"D:\\StackOverFlow\\JAXBTest\\src\\skus.xml");
parser.parse(inputStream);
System.out.println("parsed");
}
}
并且
package com.teste;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SAXXMLHANDLER extends DefaultHandler {
private List<XMLSetAdd> setAdds;
private String tempVal;
// to maintain context
private XMLSetAdd setAdd;
public SAXXMLHANDLER() {
setAdds = new ArrayList<XMLSetAdd>();
}
public List<XMLSetAdd> getIds() {
return setAdds;
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
tempVal = "";
if (qName.equals("skus")){
}else if (qName.equals("item")){
if(setAdd!=null){
setAdds.add(setAdd);
}
setAdd = new XMLSetAdd();
}
}
public void characters(char[] ch, int start, int length)
throws SAXException {
tempVal = new String(ch, start, length);
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equals("id")) {
setAdd.setId(tempVal);
} else if (qName.equals("size")){
setAdd.setSize(tempVal);
}
}
}
另一个类没有变化..我已经在Java中完成了删除main方法并运行它将起作用