亚洲精品中文字幕无乱码_久久亚洲精品无码AV大片_最新国产免费Av网址_国产精品3级片

XML

實現(xiàn)xml文件解析三種方式

時間:2024-10-30 05:47:20 XML 我要投稿
  • 相關推薦

實現(xiàn)xml文件解析三種方式

  1.sax方式

  復制代碼 代碼如下:

  /**

  * 使用sax解析

  */

  public class SaxParse{

  /**

  * sax解析器

  */

  private SAXParser parser;

  public SaxParse(){

  try {

  SAXParserFactory f = SAXParserFactory.newInstance();

  parser = f.newSAXParser();

  } catch (ParserConfigurationException e) {

  e.printStackTrace();

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  public ListdoParse(InputStream is) {

  try {

  XmlHandler h = new XmlHandler();

  parser.parse(is,h);

  return h.getpersons();

  } catch (Exception e) {

  e.printStackTrace();

  }

  return null;

  }

  /**

  * 處理器

  */

  class XmlHandler extends DefaultHandler{

  Listpersons = null ;

  Person person = null ;

  //當前元素名稱

  private String currEleName;

  /**

  * 文本節(jié)點觸發(fā)該方法

  */

  public void characters(char[] ch, int start, int length)throws SAXException {

  String str = new String(ch,start,length);

  //name

  if("name".equals(currEleName)){

  person.name = str ;

  }

  else if("age".equals(currEleName)){

  person.age = Integer.parseInt(str);

  }

  }

  public void endDocument() throws SAXException {

  }

  /**

  * 元素結(jié)束

  */

  public void endElement(String uri, String localName, String qName)

  throws SAXException {

  if("person".equals(localName)){

  persons.add(person);

  }

  //將當前元素置空

  else if(("name".equals(currEleName)) || ("age".equals(currEleName))){

  this.currEleName = "" ;

  }

  }

  /**

  * 文檔開始事件

  */

  public void startDocument() throws SAXException {

  persons = new ArrayList();

  }

  /**

  * 元素開始事件

  * localName:本地名

  * uri:名字空間

  * qName:限定名,前綴 + 本地名

  */

  public void startElement(String uri, String localName, String qName,

  Attributes attributes) throws SAXException {

  //實例化person對象

  if("person".equals(localName)){

  person = new Person();

  person.id = Integer.parseInt(attributes.getValue(0));

  }

  //name元素

  else if("name".equals(localName)){

  this.currEleName = "name" ;

  }

  //name元素

  else if("age".equals(localName)){

  this.currEleName = "age" ;

  }

  }

  public Listgetpersons(){

  return persons ;

  }

  }

  }

  2.dom方式

  復制代碼 代碼如下:

  /**

  * DOM解析

  */

  public class DomParse{

  //

  private DocumentBuilder builder;

  public DomParse(){

  try {

  DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();

  this.builder = f.newDocumentBuilder();

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  public ListdoParse(InputStream is) {

  Listpersons = new ArrayList();

  Person person = null ;

  try {

  Document doc = builder.parse(is);

  NodeList list = doc.getElementsByTagName("person");

  Element ele = null ;

  for(int i = 0 ; i < list.getLength() ; i ++){

  ele = (Element) list.item(i);

  person = new Person();

  person.id = Integer.parseInt(ele.getAttribute("id"));

  person.name = getSubElementTextContent(ele,"name");

  person.age = Integer.parseInt(getSubElementTextContent(ele,"age"));

  persons.add(person);

  }

  } catch (Exception e) {

  e.printStackTrace();

  }

  return persons;

  }

  /**

  * 得到指定的資源中中間的文本內(nèi)容

  */

  private String getSubElementTextContent(Element ele, String tagName) {

  NodeList list = ele.getElementsByTagName(tagName);

  Element e = (Element) list.item(0);

  //得到中間的文本節(jié)點

  return e.getTextContent();

  }

  }

  3.pull方式

  復制代碼 代碼如下:

  /**

  * pull解析,拉模式,可以手動控制下一個事件是否觸發(fā).

  */

  public class PullParse{

  public ListdoParse(InputStream is) {

  Listpersons = null ;

  Person person = null ;

  try {

  XmlPullParser parser = Xml.newPullParser();

  //設置解析數(shù)據(jù)源

  parser.setInput(is, "utf-8");

  //取得事件的類型

  int eventType = parser.getEventType();

  String eleName = null ;

  while(eventType != XmlPullParser.END_DOCUMENT){

  switch(eventType){

  //文檔開始

  case XmlPullParser.START_DOCUMENT:

  persons = new ArrayList();

  break ;

  //元素開始

  case XmlPullParser.START_TAG:

  eleName = parser.getName();

  if("person".equals(eleName)){

  person = new Person();

  person.id = Integer.parseInt(parser.getAttributeValue(0));

  }

  else if("name".equals(eleName)){

  person.name = parser.nextText();

  }

  else if("age".equals(eleName)){

  person.age = Integer.parseInt(parser.nextText());

  }

  break ;

  //標記結(jié)束

  case XmlPullParser.END_TAG:

  eleName = parser.getName();

  if("person".equals(eleName)){

  persons.add(person);

  }

  break ;

  }

  //手動激活下個事件的觸發(fā)

  eventType = parser.next();

  }

  } catch (Exception e) {

  e.printStackTrace();

  }

  return persons;

  }

  }

【實現(xiàn)xml文件解析三種方式】相關文章:

怎么打開xml文件的方法和軟件06-17

2023年小語種高校三種招生方式06-29

Word文件格式無效或文件已損壞怎么辦12-07

勒索病毒文件恢復201707-16

法律文件長句翻譯技巧08-30

武術禮儀解析07-23

武術分類解析12-05

holiday的詞匯解析06-11

演講的技巧解析04-19

競走的正確方式08-16