package com.zyq.service;
import java.io.OutputStream;
import java.util.List;
import org.xmlpull.v1.XmlSerializer;
import android.util.Xml;
import com.zyq.voo.Person;
/**
* 采用PULL 生成XML数据
* @author Administrator
*
*/
public class PullBuildXMLService
{
/**
*
* @param persons
* @param outputStream
* @throws Exception
*/
public void buildXML(List<Person> persons,OutputStream outputStream) throws Exception
{
XmlSerializer serializer=Xml.newSerializer();
serializer.setOutput(outputStream, "utf-8"); //转码
serializer.startDocument("utf-8", true);
serializer.startTag(null, "persons");
for(Person person:persons)
{
serializer.startTag(null, "person");
serializer.attribute(null, "id", person.getId().toString());
serializer.startTag(null, "name");
serializer.text(person.getName());
serializer.endTag(null, "name");
serializer.startTag(null, "age");
serializer.text(person.getAge().toString());
serializer.endTag(null, "age");
serializer.endTag(null, "person");
}
serializer.endTag(null, "persons");
serializer.endDocument();
outputStream.close();
}
}
打开的结果:
采用官方PULL解析器解析XML数据
package com.zyq.service;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import android.util.Xml;
import com.zyq.voo.Person;
/**
* 采用官方PULL解析器解析XML数据
* @author Administrator
*
*/
public class PullPersonService
{
/**
* 采用官方PULL解析器解析XML数据
* @param inputStream person.xml
* @return persons List集合
* @throws Throwable
*/
public List<Person> getPersons(InputStream inputStream) throws Throwable
{
List<Person> persons=null;
Person person=null;
XmlPullParser parser=Xml.newPullParser();
parser.setInput(inputStream, "utf-8");
int event=parser.getEventType();
while(event!=XmlPullParser.END_DOCUMENT)
{
switch (event)
{
case XmlPullParser.START_DOCUMENT:
persons=new ArrayList<Person>();
break;
case XmlPullParser.START_TAG:
if("person".equals(parser.getName()))
{
person=new Person();
Integer id=new Integer(parser.getAttributeValue(0));
person.setId(id);
}
if(person!=null)
{
if("name".equals(parser.getName()))
{
String name=parser.nextText().toString();
person.setName(name);
}
else if("age".equals(parser.getName()))
{
Short age=new Short(parser.nextText().toString());
person.setAge(age);
}
}
break;
case XmlPullParser.END_TAG:
if("person".equals(parser.getName()))
{
persons.add(person);
person=null;
}
break;
default:
break;
}
event=parser.next();
}
return persons;
}
}
package com.domxmltest;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class CreateXML {
public static void main(String[] args) {
Document doc;
//声明xml文档中的各个元素对象
Element students,student;
Element name=null;
Element address=null;
Element tel=null;
Element email=null;
try{
//得到dom解析器的工厂实例
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
//从dom工厂实例获得dom解析器
DocumentBuilder builder=factory.newDocumentBuilder();
//创建文档树模型对象
doc=builder.newDocument();
//如果创建的文档树模型不为空
if(null!=doc){
//创建students元素
students=doc.createElement(“students”);
//创建student元素
student=doc.createElement(“student”);
//设置student元素的属性id的值为95003
student.setAttribute(“id”, “95003″);
//将student元素添加为students的子元素
students.appendChild(student);
//创建name元素
name=doc.createElement(“name”);
//将一个文本结点添加为name元素的子结点
name.appendChild(doc.createTextNode(“Mr. Zhang”));
//将name元素添加为student的子元素
student.appendChild(name);
address=doc.createElement(“address”);
address.appendChild(doc.createTextNode(“Shangdong Qingdao”));
student.appendChild(address);
tel=doc.createElement(“tel”);
tel.appendChild(doc.createTextNode(“(0532)888888″));
student.appendChild(tel);
email=doc.createElement(“email”);
email.appendChild(doc.createTextNode(“[email protected]”));
student.appendChild(email);
//将students元素作为根元素添加到xml文档树中
doc.appendChild(students);
//将内存中的文档树保存为students.xml文档
Transformer transformer=TransformerFactory.newInstance().newTransformer();//得到转换器
//设置换行
transformer.setOutputProperty(OutputKeys.INDENT, “yes”);
//写入文件
transformer.transform(new DOMSource(doc), new StreamResult(new File(“d:/student.xml”)));
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
采用DOM完成XML的解析
package com.zyq.service;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.zyq.voo.Person;
/**
* 采用DOM完成XML的解析
* @author Administrator
*
*/
public class DOMPersonService
{
/**
* DOM 解析person.xml
* @param inputStream person.xml数据
* @return persons
* @throws Exception
*/
public List<Person> getPersons(InputStream inputStream) throws Exception
{
List<Person> persons=new ArrayList<Person>();
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document document=builder.parse(inputStream);
Element element=document.getDocumentElement();
NodeList nodeList=element.getElementsByTagName("person");
for(int i=0;i<nodeList.getLength();i++)
{
Element personNode=(Element)nodeList.item(i);
Integer id=new Integer(personNode.getAttribute("id"));
Person person=new Person();
person.setId(id);
NodeList childNodes=personNode.getChildNodes();
for(int j=0;j<childNodes.getLength();i++)
{
if(childNodes.item(j).getNodeType()==Node.ELEMENT_NODE)
{
if("name".equals(childNodes.item(j).getNodeName()))
{
String name=childNodes.item(j).getFirstChild().getNodeName();
person.setName(name);
}
else if("age".equals(childNodes.item(j).getNodeName()))
{
Short age=new Short(childNodes.item(j).getFirstChild().getNodeName());
person.setAge(age);
}
}
}
persons.add(person);
}
return persons;
}
}
SAX解析person.xml
package com.zyq.service;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.zyq.voo.Person;
public class SAXPersonService
{
/**
* SAX解析person.xml
* @param inputStream person.xml数据
* @return persons
* @throws Throwable
*/
public List<Person> getPersons(InputStream inputStream) throws Throwable
{
SAXParserFactory factory=SAXParserFactory.newInstance();
SAXParser parser=factory.newSAXParser();
PersonHandel personHandel=new PersonHandel();
parser.parse(inputStream, personHandel);
return personHandel.getPersons();
}
private class PersonHandel extends DefaultHandler
{
private List<Person> persons;
private String preTAG;
private Person person;
public List<Person> getPersons()
{
return persons;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException
{
if(preTAG!=null)
{
String data=new String(ch, start, length);
if("name".equals(preTAG))
{
person.setName(data);
}
else if("age".equals(preTAG))
{
person.setAge(new Short(data));
}
}
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException
{
if("person".equals(localName))
{
person=new Person();
Integer id= new Integer(attributes.getValue(0));
person.setId(id);
}
preTAG=localName;
}
@Override
public void startDocument() throws SAXException
{
persons=new ArrayList<Person>();
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException
{
if("person".equals(localName))
{
persons.add(person);
person=null;
}
preTAG=null;
}
}
}