android.util.XML介绍

举报
ShaderJoy 发表于 2021/12/30 02:06:40 2021/12/30
【摘要】 android为我们提供了一系列的和XML操作相关的方法,这些方法都位于类android.util.XML中,主要方法如下 asAttributeSet()方法:       将xml中的内容以加载到一个AttributeSet中,以键值对的形式存储。一般被用于描述某个图...

android为我们提供了一系列的和XML操作相关的方法,这些方法都位于类android.util.XML中,主要方法如下

asAttributeSet()方法:

      将xml中的内容以加载到一个AttributeSet中,以键值对的形式存储。一般被用于描述某个图形表现形式。和android:text这种标签的功能一样。

其他的方法也没什么好解释的了。

这里粘贴处XML类的源代码,对于其实现过程就一目了然了


  
  1. /*
  2. * Copyright (C) 2007 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package android.util;
  17. import org.xml.sax.ContentHandler;
  18. import org.xml.sax.InputSource;
  19. import org.xml.sax.SAXException;
  20. import org.xml.sax.XMLReader;
  21. import org.xmlpull.v1.XmlPullParser;
  22. import org.xmlpull.v1.XmlSerializer;
  23. import org.xmlpull.v1.XmlPullParserException;
  24. import org.xmlpull.v1.XmlPullParserFactory;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.io.Reader;
  28. import java.io.StringReader;
  29. import java.io.UnsupportedEncodingException;
  30. import org.apache.harmony.xml.ExpatPullParser;
  31. import org.apache.harmony.xml.ExpatReader;
  32. /**
  33. * XML utility methods.
  34. */
  35. public class Xml {
  36. /**
  37. * {@link org.xmlpull.v1.XmlPullParser} "relaxed" feature name.
  38. *
  39. * @see <a href="http://xmlpull.org/v1/doc/features.html#relaxed" mce_href="http://xmlpull.org/v1/doc/features.html#relaxed">
  40. * specification</a>
  41. */
  42. public static String FEATURE_RELAXED = ExpatPullParser.FEATURE_RELAXED;
  43. /**
  44. * Parses the given xml string and fires events on the given SAX handler.
  45. */
  46. public static void parse(String xml, ContentHandler contentHandler)
  47. throws SAXException {
  48. try {
  49. XMLReader reader = new ExpatReader();
  50. reader.setContentHandler(contentHandler);
  51. reader.parse(new InputSource(new StringReader(xml)));
  52. }
  53. catch (IOException e) {
  54. throw new AssertionError(e);
  55. }
  56. }
  57. /**
  58. * Parses xml from the given reader and fires events on the given SAX
  59. * handler.
  60. */
  61. public static void parse(Reader in, ContentHandler contentHandler)
  62. throws IOException, SAXException {
  63. XMLReader reader = new ExpatReader();
  64. reader.setContentHandler(contentHandler);
  65. reader.parse(new InputSource(in));
  66. }
  67. /**
  68. * Parses xml from the given input stream and fires events on the given SAX
  69. * handler.
  70. */
  71. public static void parse(InputStream in, Encoding encoding,
  72. ContentHandler contentHandler) throws IOException, SAXException {
  73. try {
  74. XMLReader reader = new ExpatReader();
  75. reader.setContentHandler(contentHandler);
  76. InputSource source = new InputSource(in);
  77. source.setEncoding(encoding.expatName);
  78. reader.parse(source);
  79. } catch (IOException e) {
  80. throw new AssertionError(e);
  81. }
  82. }
  83. /**
  84. * Creates a new pull parser with namespace support.
  85. *
  86. * <p><b>Note:</b> This is actually slower than the SAX parser, and it's not
  87. * fully implemented. If you need a fast, mostly implemented pull parser,
  88. * use this. If you need a complete implementation, use KXML.
  89. */
  90. public static XmlPullParser newPullParser() {
  91. ExpatPullParser parser = new ExpatPullParser();
  92. parser.setNamespaceProcessingEnabled(true);
  93. return parser;
  94. }
  95. /**
  96. * Creates a new xml serializer.
  97. */
  98. public static XmlSerializer newSerializer() {
  99. try {
  100. return XmlSerializerFactory.instance.newSerializer();
  101. } catch (XmlPullParserException e) {
  102. throw new AssertionError(e);
  103. }
  104. }
  105. /** Factory for xml serializers. Initialized on demand. */
  106. static class XmlSerializerFactory {
  107. static final String TYPE
  108. = "org.kxml2.io.KXmlParser,org.kxml2.io.KXmlSerializer";
  109. static final XmlPullParserFactory instance;
  110. static {
  111. try {
  112. instance = XmlPullParserFactory.newInstance(TYPE, null);
  113. } catch (XmlPullParserException e) {
  114. throw new AssertionError(e);
  115. }
  116. }
  117. }
  118. /**
  119. * Supported character encodings.
  120. */
  121. public enum Encoding {
  122. US_ASCII("US-ASCII"),
  123. UTF_8("UTF-8"),
  124. UTF_16("UTF-16"),
  125. ISO_8859_1("ISO-8859-1");
  126. final String expatName;
  127. Encoding(String expatName) {
  128. this.expatName = expatName;
  129. }
  130. }
  131. /**
  132. * Finds an encoding by name. Returns UTF-8 if you pass {@code null}.
  133. */
  134. public static Encoding findEncodingByName(String encodingName)
  135. throws UnsupportedEncodingException {
  136. if (encodingName == null) {
  137. return Encoding.UTF_8;
  138. }
  139. for (Encoding encoding : Encoding.values()) {
  140. if (encoding.expatName.equalsIgnoreCase(encodingName))
  141. return encoding;
  142. }
  143. throw new UnsupportedEncodingException(encodingName);
  144. }
  145. /**
  146. * Return an AttributeSet interface for use with the given XmlPullParser.
  147. * If the given parser itself implements AttributeSet, that implementation
  148. * is simply returned. Otherwise a wrapper class is
  149. * instantiated on top of the XmlPullParser, as a proxy for retrieving its
  150. * attributes, and returned to you.
  151. *
  152. * @param parser The existing parser for which you would like an
  153. * AttributeSet.
  154. *
  155. * @return An AttributeSet you can use to retrieve the
  156. * attribute values at each of the tags as the parser moves
  157. * through its XML document.
  158. *
  159. * @see AttributeSet
  160. */
  161. public static AttributeSet asAttributeSet(XmlPullParser parser) {
  162. return (parser instanceof AttributeSet)
  163. ? (AttributeSet) parser
  164. : new XmlPullAttributes(parser);
  165. }
  166. }


文章来源: panda1234lee.blog.csdn.net,作者:panda1234lee,版权归原作者所有,如需转载,请联系作者。

原文链接:panda1234lee.blog.csdn.net/article/details/8831947

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。