android.util.XML介紹

來源:互聯網
上載者:User

android為我們提供了一系列的和XML操作相關的方法,這些方法都位於類android.util.XML中,主要方法如下

asAttributeSet()方法:

      將xml中的內容以載入到一個AttributeSet中,以索引值對的形式儲存。一般被用於描述某個圖形表現形式。和android:text這種標籤的功能一樣。

其他的方法也沒什麼好解釋的了。

這裡粘貼處XML類的原始碼,對於其實現過程就一目瞭然了、

 

 

 

 /*<br /> * Copyright (C) 2007 The Android Open Source Project<br /> *<br /> * Licensed under the Apache License, Version 2.0 (the "License");<br /> * you may not use this file except in compliance with the License.<br /> * You may obtain a copy of the License at<br /> *<br /> * http://www.apache.org/licenses/LICENSE-2.0<br /> *<br /> * Unless required by applicable law or agreed to in writing, software<br /> * distributed under the License is distributed on an "AS IS" BASIS,<br /> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.<br /> * See the License for the specific language governing permissions and<br /> * limitations under the License.<br /> */</p><p>package android.util;</p><p>import org.xml.sax.ContentHandler;<br />import org.xml.sax.InputSource;<br />import org.xml.sax.SAXException;<br />import org.xml.sax.XMLReader;<br />import org.xmlpull.v1.XmlPullParser;<br />import org.xmlpull.v1.XmlSerializer;<br />import org.xmlpull.v1.XmlPullParserException;<br />import org.xmlpull.v1.XmlPullParserFactory;</p><p>import java.io.IOException;<br />import java.io.InputStream;<br />import java.io.Reader;<br />import java.io.StringReader;<br />import java.io.UnsupportedEncodingException;</p><p>import org.apache.harmony.xml.ExpatPullParser;<br />import org.apache.harmony.xml.ExpatReader;</p><p>/**<br /> * XML utility methods.<br /> */<br />public class Xml {</p><p> /**<br /> * {@link org.xmlpull.v1.XmlPullParser} "relaxed" feature name.<br /> *<br /> * @see <a href="http://xmlpull.org/v1/doc/features.html#relaxed" mce_href="http://xmlpull.org/v1/doc/features.html#relaxed"><br /> * specification</a><br /> */<br /> public static String FEATURE_RELAXED = ExpatPullParser.FEATURE_RELAXED;</p><p> /**<br /> * Parses the given xml string and fires events on the given SAX handler.<br /> */<br /> public static void parse(String xml, ContentHandler contentHandler)<br /> throws SAXException {<br /> try {<br /> XMLReader reader = new ExpatReader();<br /> reader.setContentHandler(contentHandler);<br /> reader.parse(new InputSource(new StringReader(xml)));<br /> }<br /> catch (IOException e) {<br /> throw new AssertionError(e);<br /> }<br /> }</p><p> /**<br /> * Parses xml from the given reader and fires events on the given SAX<br /> * handler.<br /> */<br /> public static void parse(Reader in, ContentHandler contentHandler)<br /> throws IOException, SAXException {<br /> XMLReader reader = new ExpatReader();<br /> reader.setContentHandler(contentHandler);<br /> reader.parse(new InputSource(in));<br /> }</p><p> /**<br /> * Parses xml from the given input stream and fires events on the given SAX<br /> * handler.<br /> */<br /> public static void parse(InputStream in, Encoding encoding,<br /> ContentHandler contentHandler) throws IOException, SAXException {<br /> try {<br /> XMLReader reader = new ExpatReader();<br /> reader.setContentHandler(contentHandler);<br /> InputSource source = new InputSource(in);<br /> source.setEncoding(encoding.expatName);<br /> reader.parse(source);<br /> } catch (IOException e) {<br /> throw new AssertionError(e);<br /> }<br /> }</p><p> /**<br /> * Creates a new pull parser with namespace support.<br /> *<br /> * <p><b>Note:</b> This is actually slower than the SAX parser, and it's not<br /> * fully implemented. If you need a fast, mostly implemented pull parser,<br /> * use this. If you need a complete implementation, use KXML.<br /> */<br /> public static XmlPullParser newPullParser() {<br /> ExpatPullParser parser = new ExpatPullParser();<br /> parser.setNamespaceProcessingEnabled(true);<br /> return parser;<br /> }</p><p> /**<br /> * Creates a new xml serializer.<br /> */<br /> public static XmlSerializer newSerializer() {<br /> try {<br /> return XmlSerializerFactory.instance.newSerializer();<br /> } catch (XmlPullParserException e) {<br /> throw new AssertionError(e);<br /> }<br /> }</p><p> /** Factory for xml serializers. Initialized on demand. */<br /> static class XmlSerializerFactory {<br /> static final String TYPE<br /> = "org.kxml2.io.KXmlParser,org.kxml2.io.KXmlSerializer";<br /> static final XmlPullParserFactory instance;<br /> static {<br /> try {<br /> instance = XmlPullParserFactory.newInstance(TYPE, null);<br /> } catch (XmlPullParserException e) {<br /> throw new AssertionError(e);<br /> }<br /> }<br /> }</p><p> /**<br /> * Supported character encodings.<br /> */<br /> public enum Encoding {</p><p> US_ASCII("US-ASCII"),<br /> UTF_8("UTF-8"),<br /> UTF_16("UTF-16"),<br /> ISO_8859_1("ISO-8859-1");</p><p> final String expatName;</p><p> Encoding(String expatName) {<br /> this.expatName = expatName;<br /> }<br /> }</p><p> /**<br /> * Finds an encoding by name. Returns UTF-8 if you pass {@code null}.<br /> */<br /> public static Encoding findEncodingByName(String encodingName)<br /> throws UnsupportedEncodingException {<br /> if (encodingName == null) {<br /> return Encoding.UTF_8;<br /> }</p><p> for (Encoding encoding : Encoding.values()) {<br /> if (encoding.expatName.equalsIgnoreCase(encodingName))<br /> return encoding;<br /> }<br /> throw new UnsupportedEncodingException(encodingName);<br /> }</p><p> /**<br /> * Return an AttributeSet interface for use with the given XmlPullParser.<br /> * If the given parser itself implements AttributeSet, that implementation<br /> * is simply returned. Otherwise a wrapper class is<br /> * instantiated on top of the XmlPullParser, as a proxy for retrieving its<br /> * attributes, and returned to you.<br /> *<br /> * @param parser The existing parser for which you would like an<br /> * AttributeSet.<br /> *<br /> * @return An AttributeSet you can use to retrieve the<br /> * attribute values at each of the tags as the parser moves<br /> * through its XML document.<br /> *<br /> * @see AttributeSet<br /> */<br /> public static AttributeSet asAttributeSet(XmlPullParser parser) {<br /> return (parser instanceof AttributeSet)<br /> ? (AttributeSet) parser<br /> : new XmlPullAttributes(parser);<br /> }<br />}<br />

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.