1. <tt id="5hhch"><source id="5hhch"></source></tt>
    1. <xmp id="5hhch"></xmp>

  2. <xmp id="5hhch"><rt id="5hhch"></rt></xmp>

    <rp id="5hhch"></rp>
        <dfn id="5hhch"></dfn>

      1. 實現xml文件解析三種方式

        時間:2024-10-14 13:22:54 XML 我要投稿
        • 相關推薦

        實現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;

          /**

          * 文本節點觸發該方法

          */

          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 {

          }

          /**

          * 元素結束

          */

          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;

          }

          /**

          * 得到指定的資源中中間的文本內容

          */

          private String getSubElementTextContent(Element ele, String tagName) {

          NodeList list = ele.getElementsByTagName(tagName);

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

          //得到中間的文本節點

          return e.getTextContent();

          }

          }

          3.pull方式

          復制代碼 代碼如下:

          /**

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

          */

          public class PullParse{

          public ListdoParse(InputStream is) {

          Listpersons = null ;

          Person person = null ;

          try {

          XmlPullParser parser = Xml.newPullParser();

          //設置解析數據源

          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 ;

          //標記結束

          case XmlPullParser.END_TAG:

          eleName = parser.getName();

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

          persons.add(person);

          }

          break ;

          }

          //手動激活下個事件的觸發

          eventType = parser.next();

          }

          } catch (Exception e) {

          e.printStackTrace();

          }

          return persons;

          }

          }

        【實現xml文件解析三種方式】相關文章:

        Java多線程的實現方式07-08

        php解析ini配置文件07-13

        新西蘭留學住宿的三種方式07-01

        dos實現文件夾樹目錄生成09-16

        高考后留學美國的方式解析07-24

        SAT語法考試的出題方式解析01-20

        網站優化要實現內容的聚合的方式10-01

        高爾夫三種基本握桿方式06-10

        愛爾蘭獎學金發放的三種方式08-09

        寶寶表達情感需求的三種方式08-20

        国产高潮无套免费视频_久久九九兔免费精品6_99精品热6080YY久久_国产91久久久久久无码

        1. <tt id="5hhch"><source id="5hhch"></source></tt>
          1. <xmp id="5hhch"></xmp>

        2. <xmp id="5hhch"><rt id="5hhch"></rt></xmp>

          <rp id="5hhch"></rp>
              <dfn id="5hhch"></dfn>