自从有了JSON解析后,好像XML很少用到了,不过也会知道,以后说不定又用上了
<?xml version="1.0" encoding="utf-8"?>
<persons>
<person id="12">
<name>zhangshan</name>
<age>23</age>
</person>
<person id="13">
<name>lishi</name>
<age>24</age>
</person>
</persons>
第一种DOM解析
public class DomPersonService {
public static List<Person> getPersons(InputStream inStream) throws Exception{
List<Person> persons = new ArrayList<Person>();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(inStream);
Element element =document.getDocumentElement();
NodeList nodelist = element.getElementsByTagName("person");
for (int i = 0; i < nodelist.getLength(); i++) {
Person person = new Person();
Element personElement = (Element) nodelist.item(i);
person.setId(new Integer(personElement.getAttribute("id")));
NodeList childerList = personElement.getChildNodes();
for (int j = 0; j < childerList.getLength(); j++) {
if(childerList.item(j).getNodeType() == Node.ELEMENT_NODE){//判断当前节点是否是元素内容的节点
Element childerElement = (Element) childerList.item(j);
if("name".equals(childerElement.getNodeName())){
person.setName(childerElement.getFirstChild().getNodeValue());
}else if("age".equals(childerElement.getNodeName())){
person.setAge(new Short(childerElement.getFirstChild().getNodeValue()));
}
}
}
persons.add(person);
}
return persons;
}
第二种SAX解析
public class SAXParser {
public List<Person> getPersons(InputStream inStream) throws Exception{
SAXParserFactory factory = SAXParserFactory.newInstance();
javax.xml.parsers.SAXParser parser = factory.newSAXParser();
PersonParser personParser = new PersonParser();
parser.parse(inStream,personParser);
return personParser.getPersons();
}
private final class PersonParser extends DefaultHandler{
private List<Person> persons = null;
private String currentTag = null;
Person person = null;
public List<Person> getPersons(){
return persons;
}
@Override
public void startDocument() throws SAXException {
persons = new ArrayList<Person>();
}
@Override
public void startElement(String uri,String localName,String qName,
Attributes attributes) throws SAXException {
if("person".equals(localName)){
person = new Person();
person.setId(new Integer(attributes.getValue(0)));
}
currentTag = localName;
}
@Override
public void endElement(String uri,String qName)
throws SAXException {
if("person".equals(localName)){
persons.add(person);
person = null;
}
currentTag = null;
}
@Override
public void characters(char[] ch,int start,int length)
throws SAXException {
if(currentTag != null){
String data = new String(ch,start,length);
if("name".equals(currentTag)){
person.setName(data);
}else if("age".equals(currentTag)){
person.setAge(new Short(data));
}
}
}
}
}
第三种Pull解析
public class Pullxml {
public static List<Person> getPersons(InputStream inStream)throws Exception{
List<Person> persons = null;
Person person = null;
XmlPullParser parser = Xml.newPullParser();
parser.setInput(inStream,"UTF-8");
int eventType = parser.getEventType();
while(eventType!=XmlPullParser.END_DOCUMENT)
{
switch(eventType)
{
case XmlPullParser.START_DOCUMENT:
persons = new ArrayList<Person>();
break;
case XmlPullParser.START_TAG:
String str = parser.getName();
if((str!=null&&str.equals("person")))
{
person = new Person();
person.setId(new Integer(parser.getAttributeValue(0)));
}if(person != null){
if("name".equals(str)){
person.setName(parser.nextText());
}
if("age".equals(str)){
person.setAge(new Short(parser.nextText()));
}
}
break;
case XmlPullParser.END_TAG:
if("person".equals(parser.getName())){
persons.add(person);
person = null;
}
break;
}
eventType = parser.next();
}
return persons;
}
}