android plist解析器

【android plist解析器】弓背霞明剑照霜,秋风走马出咸阳。这篇文章主要讲述android plist解析器相关的知识,希望能为你提供帮助。

  1. import java.io.IOException;
  2. import java.io.StringReader;
  3. import java.util.ArrayList;
  4.  
  5. import javax.xml.parsers.DocumentBuilder;
  6. import javax.xml.parsers.DocumentBuilderFactory;
  7. import javax.xml.parsers.ParserConfigurationException;
  8.  
  9. import org.w3c.dom.Document;
  10. import org.w3c.dom.Element;
  11. import org.w3c.dom.Node;
  12. import org.w3c.dom.NodeList;
  13. import org.xml.sax.InputSource;
  14. import org.xml.sax.SAXException;
  15.  
  16.  
  17. import com.gtomato.disneyhalloween2012.pojo.MapPin;
  18.  
  19. public class PlistParser {
  20. // parse Plist and fill in arraylist
  21. public ArrayList< MapPin> parsePlist(String xml) {
  22. final ArrayList< MapPin> dataModels = new ArrayList< MapPin> ();
  23. // Get the xml string from assets
  24.  
  25. final Document doc = XMLfromString(xml);
  26.  
  27. final NodeList nodes_array = doc.getElementsByTagName(" dict" );
  28.  
  29. // Fill in the list items from the XML document
  30. for (int index = 0; index < nodes_array.getLength( ) ; index++) {
  31.  
  32. final Node node = nodes_array.item( index) ;
  33.  
  34. if ( node.getNodeType( ) == Node.ELEMENT_NODE) {
  35. final Element e = ( Element) nodes_array.item( index) ;
  36.  
  37. final NodeList nodeKey = e.getElementsByTagName( " key" ) ;
  38. final NodeList nodeValue = https://www.songbingjia.com/android/e.getElementsByTagName( " string" ) ;
  39. //MapPin model = new MapPin( ) ;
  40.  
  41. for ( int i = 0; i < nodeValue.getLength( ) ; i++) {
  42.  
  43. final Element eleKey = ( Element) nodeKey.item( i) ;
  44. final Element eleString = ( Element) nodeValue.item( i) ;
  45.  
  46. if ( eleString != null) {
  47.  
  48. String strValue = https://www.songbingjia.com/android/getValue( eleString, " string" ) ;
  49.  
  50. if ( getValue( eleKey, " key" ) .equals( " gameid" ) ) {
  51. //model = new MapPin( ) ;
  52. //model.setGameId( Integer.parseInt( strValue) ) ;
  53. } else if ( getValue( eleKey, " key" ) .equals( " drawid" ) ) {
  54. //model.setDrawId( Integer.parseInt( strValue) ) ;
  55. } else if ( getValue( eleKey, " key" ) .equals( " dname" ) ) {
  56. //model.setDrawName( strValue) ;
  57. } else if ( getValue( eleKey, " key" ) .equals( " date" ) ) {
  58. //model.setDate( strValue) ;
  59. } else if ( getValue( eleKey, " key" ) .equals( " prize" ) ) {
  60. //model.setPrize( strValue) ;
  61. } else if ( getValue( eleKey, " key" ) .equals( " sels" ) ) {
  62. if ( strValue =https://www.songbingjia.com/android/= null) {
  63. strValue = https://www.songbingjia.com/android/" " ;
  64. }
  65. //model.setSels( strValue) ;
  66. //dataModels.add( model) ;
  67. }
  68. }
  69. }
  70. }
  71. }
  72. return dataModels;
  73. }
  74.  
  75. // Create xml document object from XML String
  76. private Document XMLfromString( String xml) {
  77. Document doc = null;
  78.  
  79. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance( ) ;
  80. try {
  81. DocumentBuilder db = dbf.newDocumentBuilder( ) ;
  82. InputSource is = new InputSource( ) ;
  83. is.setCharacterStream( new StringReader( xml) ) ;
  84. doc = db.parse( is) ;
  85. } catch ( ParserConfigurationException e) {
  86. System.out.println( " XML parse error: " + e.getMessage( ) ) ;
  87. return null;
  88. } catch ( SAXException e) {
  89. System.out.println( " Wrong XML file structure: " + e.getMessage( ) ) ;
  90. return null;
  91. } catch ( IOException e) {
  92. System.out.println( " I/O exeption: " + e.getMessage( ) ) ;
  93. return null;
  94. }
  95.  
  96. return doc;
  97. }
  98.  
  99. // fetch value from Text Node only
  100. private String getElementValue( Node elem) {
  101. Node kid;
  102. if ( elem != null) {
  103. if ( elem.hasChildNodes( ) ) {
  104. for ( kid = elem.getFirstChild( ) ; kid != null; kid = kid
  105. .getNextSibling( ) ) {
  106. if ( kid.getNodeType( ) == Node.TEXT_NODE) {
  107. return kid.getNodeValue( ) ;
  108. }
  109. }
  110. }
  111. }
  112. return " " ;
  113. }
  114.  
  115. // / Fetch value from XML Node
  116. private String getValue( Element item, String str) {
  117. NodeList n = item.getElementsByTagName( str) ;
  118. return getElementValue( n.item( 0) ) ;
  119. }
  120. }


    推荐阅读