zoukankan      html  css  js  c++  java
  • xml解析

    首先是xml文档:

    <types>
        <type id = "1">
            <name>name1</name>
        </type>
         <type id = "2">
            <name>name2</name>
        </type>
          <type id = "3">
            <name>name3</name>
        </type>
         <type id = "4">
            <name>name4</name>
        </type>
         <type id = "5">
            <name>name5</name>
        </type>
    </types>

    然后是解析的方法:

    package com.example.spinnertest;
    
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    
    import android.content.Context;
    import android.util.Log;
    
    public class XMLManager {
        class Type{
            String id;
            String name;
            public Type(){
                
            }
            public String getId() {
                return id;
            }
            public void setId(String id) {
                this.id = id;
            }
            public String getName() {
                return name;
            }
            public void setName(String name) {
                this.name = name;
            }
            @Override
            public String toString() {
                // TODO Auto-generated method stub
                return name;
            }
            
        }
        public List<Type> start(Context context){
            Document doc = getDocumentFromXML(context,"types.xml");
            List<Type> result = null;
            if(doc!=null){
                result = new ArrayList<XMLManager.Type>();
                Element element = doc.getDocumentElement();  
                NodeList entry = element.getElementsByTagName("type");
                Type typeItem = null;
                for(int i=0;i<entry.getLength();i++){  
                    typeItem = new Type();
                    Element childElement = (Element) entry.item(i);
                    typeItem.setId(childElement.getAttribute("id"));
                    NodeList childNodes = childElement.getChildNodes();  
                    for(int j=0;j<childNodes.getLength();j++){  
                        Node node = childNodes.item(j);
                        if(node.getNodeType()==Node.ELEMENT_NODE){  
                            if("name".equals(node.getNodeName())){ 
                                typeItem.setName(node.getTextContent());
                            }
                        }  
                    } 
                    result.add(typeItem);
                }
            }
            return result;
        }
        public static Document getDocumentFromXML(Context context,String xmlName){
            try {
                InputStream input = context.getResources().getAssets().open(xmlName);;
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
                DocumentBuilder builder;
                
                    builder = factory.newDocumentBuilder();
                
                return builder.parse(input);  
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            return null;
        }
    
    }
  • 相关阅读:
    Python 的并发编程
    django搭建一个小型的服务器运维网站-拿来即用的bootstrap模板
    python 文件目录遍历
    Ubuntu 18.04 登陆界面进去,几秒之后自动退出到登陆界面
    terminal 快捷操作
    Boost 源代码交叉编译
    tar 常见操作
    vim 快捷设置和操作
    Visual Studio Linker选项设置
    5. glutInitDisplayMode 函数理解
  • 原文地址:https://www.cnblogs.com/qgli/p/4602287.html
Copyright © 2011-2022 走看看