【android plist解析器】弓背霞明剑照霜,秋风走马出咸阳。这篇文章主要讲述android plist解析器相关的知识,希望能为你提供帮助。
- import java.io.IOException;
- import java.io.StringReader;
- import java.util.ArrayList;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- import org.xml.sax.InputSource;
- import org.xml.sax.SAXException;
- import com.gtomato.disneyhalloween2012.pojo.MapPin;
- public class PlistParser {
- // parse Plist and fill in arraylist
- public ArrayList< MapPin> parsePlist(String xml) {
- final ArrayList< MapPin> dataModels = new ArrayList< MapPin> ();
- // Get the xml string from assets
- final Document doc = XMLfromString(xml);
- final NodeList nodes_array = doc.getElementsByTagName(" dict" );
- // Fill in the list items from the XML document
- for (int index = 0; index < nodes_array.getLength( ) ; index++) {
- final Node node = nodes_array.item( index) ;
- if ( node.getNodeType( ) == Node.ELEMENT_NODE) {
- final Element e = ( Element) nodes_array.item( index) ;
- final NodeList nodeKey = e.getElementsByTagName( " key" ) ;
- final NodeList nodeValue = https://www.songbingjia.com/android/e.getElementsByTagName( " string" ) ;
- //MapPin model = new MapPin( ) ;
- for ( int i = 0; i < nodeValue.getLength( ) ; i++) {
- final Element eleKey = ( Element) nodeKey.item( i) ;
- final Element eleString = ( Element) nodeValue.item( i) ;
- if ( eleString != null) {
- String strValue = https://www.songbingjia.com/android/getValue( eleString, " string" ) ;
- if ( getValue( eleKey, " key" ) .equals( " gameid" ) ) {
- //model = new MapPin( ) ;
- //model.setGameId( Integer.parseInt( strValue) ) ;
- } else if ( getValue( eleKey, " key" ) .equals( " drawid" ) ) {
- //model.setDrawId( Integer.parseInt( strValue) ) ;
- } else if ( getValue( eleKey, " key" ) .equals( " dname" ) ) {
- //model.setDrawName( strValue) ;
- } else if ( getValue( eleKey, " key" ) .equals( " date" ) ) {
- //model.setDate( strValue) ;
- } else if ( getValue( eleKey, " key" ) .equals( " prize" ) ) {
- //model.setPrize( strValue) ;
- } else if ( getValue( eleKey, " key" ) .equals( " sels" ) ) {
- if ( strValue =https://www.songbingjia.com/android/= null) {
- strValue = https://www.songbingjia.com/android/" " ;
- }
- //model.setSels( strValue) ;
- //dataModels.add( model) ;
- }
- }
- }
- }
- }
- return dataModels;
- }
- // Create xml document object from XML String
- private Document XMLfromString( String xml) {
- Document doc = null;
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance( ) ;
- try {
- DocumentBuilder db = dbf.newDocumentBuilder( ) ;
- InputSource is = new InputSource( ) ;
- is.setCharacterStream( new StringReader( xml) ) ;
- doc = db.parse( is) ;
- } catch ( ParserConfigurationException e) {
- System.out.println( " XML parse error: " + e.getMessage( ) ) ;
- return null;
- } catch ( SAXException e) {
- System.out.println( " Wrong XML file structure: " + e.getMessage( ) ) ;
- return null;
- } catch ( IOException e) {
- System.out.println( " I/O exeption: " + e.getMessage( ) ) ;
- return null;
- }
- return doc;
- }
- // fetch value from Text Node only
- private String getElementValue( Node elem) {
- Node kid;
- if ( elem != null) {
- if ( elem.hasChildNodes( ) ) {
- for ( kid = elem.getFirstChild( ) ; kid != null; kid = kid
- .getNextSibling( ) ) {
- if ( kid.getNodeType( ) == Node.TEXT_NODE) {
- return kid.getNodeValue( ) ;
- }
- }
- }
- }
- return " " ;
- }
- // / Fetch value from XML Node
- private String getValue( Element item, String str) {
- NodeList n = item.getElementsByTagName( str) ;
- return getElementValue( n.item( 0) ) ;
- }
- }
推荐阅读
- 使用dd-plist的Android plist解析器
- android Plist解析器xml资产
- Android MyOverlay扩展覆盖
- Android和Iphone-现代css媒体查询
- .NET-C#-Interop-Excel-Utility/Wrapper类
- Appindicator正面
- 使用AppleScript重新启动Django开发服务器
- 用于检测访问者是否在android设备上的函数
- AppStore从网站自动检测应用程序