<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                Android SDK支持采用SAX技術讀取XML,SAX采用順序讀取的方式來處理XML文檔。這就要求在每讀取XML文檔的某個節點時會觸發相應的事件來處理這個節點。下面基于一個實例講述SAX的使用: ~~~ public class Book { private String name; private String id; private String price; private String publisher; private int count; .... get,set方法省略 } ~~~ XML文件如下: ~~~ <?xml version="1.0" encoding="utf-8"?> <books xmlns:book="http://schemas.android.com/com.example.jsonxmlio"> <book book:name="語文" book:id="001" book:price="45" book:publisher="A">12</book> <book book:name="數學" book:id="002" book:price="50" book:publisher="B">10</book> <book book:name="英語" book:id="003" book:price="55" book:publisher="C">21</book> </books> ~~~ XMLTool.java 1.構建一個工廠SAXParserFactory 2.構建并實例化SAXPraser對象 ~~~ public class XMLTool { private static SAXParser getSAXParser() throws ParserConfigurationException, SAXException { SAXParserFactory parserFactory = SAXParserFactory.newInstance(); return parserFactory.newSAXParser(); } public static DefaultHandler parse(InputStream inStream,DefaultHandler handler){ if(inStream!=null){ try { SAXParser parser = getSAXParser(); parser.parse(inStream, handler); return handler; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(inStream!=null){ try { inStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } return null; } } ~~~ BookXMLParser.java ~~~ public class BookXMLParser extends DefaultHandler { private ArrayList<Book> dataList; private Book book; private StringBuffer stringBuffer = new StringBuffer(); //private StringBuffer buffer=new StringBuffer(); public ArrayList<Book> getData() { return dataList; } public void startDocument() throws SAXException { // TODO Auto-generated method stub dataList = new ArrayList<Book>(); } public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { // TODO Auto-generated method stub if(qName.equals("book")) { book = new Book(); book.setName(attributes.getValue("book:name")); book.setId(attributes.getValue("book:id")); book.setPrice(attributes.getValue("book:price")); book.setPublisher(attributes.getValue("book:publisher")); } super.startElement(uri, localName, qName, attributes); } @Override public void characters(char[] ch, int start, int length) throws SAXException { // TODO Auto-generated method stub stringBuffer.append(ch,start,length); super.characters(ch, start, length); } @Override public void endElement(String uri, String localName, String qName) throws SAXException { // TODO Auto-generated method stub if(qName.equals("book")) { if(stringBuffer.toString()!=null && !stringBuffer.toString().equals("")) { book.setCount(Integer.parseInt(stringBuffer.toString().trim())); stringBuffer.setLength(0);//必須清空緩沖區 } dataList.add(book); } super.endElement(uri, localName, qName); } } ~~~ SAX引擎需要處理5個分析點,也可以稱為分析事件。 1.開始分析XML文件。該分析點表示SAX引擎剛剛開始處理XML文件,但是還沒有讀取XML文件中的內容,該分析點對應: ~~~ public void startDocument() throws SAXException { // TODO Auto-generated method stub dataList = new ArrayList<Book>(); } ~~~ 在此方法里面可以做一些初始化的工作。 2.開始處理每一個XML元素。也就是遇到<book>這樣的起始標記的時候都會觸發這個分析節點,所對應的事件方法是startElement。在這個節點可以獲得元素的名稱、屬性的相關信息。 ~~~ public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { // TODO Auto-generated method stub if(qName.equals("book")) { book = new Book(); book.setName(attributes.getValue("book:name")); book.setId(attributes.getValue("book:id")); book.setPrice(attributes.getValue("book:price")); book.setPublisher(attributes.getValue("book:publisher")); } super.startElement(uri, localName, qName, attributes); } ~~~ 3.處理完每一個XML元素。也就是遇到</book>這樣的結束標記的時候會觸發endElement方法,在該事件中可以獲得當前處理完元素的全部信息。 ~~~ public void endElement(String uri, String localName, String qName) throws SAXException { // TODO Auto-generated method stub if(qName.equals("book")) { if(stringBuffer.toString()!=null && !stringBuffer.toString().equals("")) { book.setCount(Integer.parseInt(stringBuffer.toString().trim())); stringBuffer.setLength(0);//必須清空緩沖區 } dataList.add(book); } super.endElement(uri, localName, qName); } ~~~ 4.處理完XML文件。如果SAX引擎將整個XML文件全部掃描完就會出發endDocument方法。這個方法可能不是必須的,但在這個方法中可以完成一些收尾工作,比如說釋放資源等。在該例中我沒有使用。 5.讀取字符分析點。這是一個很重要的分析點。如果沒有這個分析點, 前面的工作相當于白做,雖然掃描了XML文件,但是沒有保存.....而這個分析點所對應的characters事件方法的主要作用就是保存SAX讀取的XML文件內容。具體的說就是<book ...? ...>12</book>中的“12” ~~~ public void characters(char[] ch, int start, int length) throws SAXException { // TODO Auto-generated method stub stringBuffer.append(ch,start,length); super.characters(ch, start, length); } ~~~ 使用SAX解析XML: ~~~ public class MainActivity extends Activity { private List<Book> books; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); InputStream inStream = getResources().openRawResource(R.raw.books); BookXMLParser parser = new BookXMLParser(); books = ((BookXMLParser)XMLTool.parse(inStream, parser)).getData(); if(books!=null && books.size()>0) { for(int i = 0;i<books.size();i++) { Log.d("AAA", books.get(i).toString()); } } } } ~~~ 寫XML文件 ~~~ public static void WriteXML(List<Book> books, OutputStream out) throws Exception { XmlSerializer serializer = Xml.newSerializer(); serializer.setOutput(out, "UTF-8"); serializer.startDocument("UTF-8", true); serializer.startTag(null, "books"); for (Book book : books) { serializer.startTag(null, "book"); serializer.attribute(null, "book:name", book.getName()); serializer.attribute(null, "book:id",book.getId()); serializer.attribute(null, "book:price", book.getPrice()); serializer.attribute(null, "book:publisher",book.getPublisher()); serializer.text(String.valueOf(book.getCount())); serializer.endTag(null, "book"); } serializer.endTag(null, "books"); serializer.endDocument(); out.flush(); out.close(); } ~~~ Demo:[http://download.csdn.net/detail/tangnengwu/7664719](http://download.csdn.net/detail/tangnengwu/7664719)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看