dom解析xml

Posted jackie-zhang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dom解析xml相关的知识,希望对你有一定的参考价值。

 public class MainActivity extends Activity { 
        
     // 存储最终在前台显示的内容 
     private String result = ""; 
     private ArrayList<Beauty> beautyList = new ArrayList<Beauty>();      
     private Beauty beauty = null; 
        
      @Override    
      public void onCreate(Bundle savedInstanceState) { 
        
        super.onCreate(savedInstanceState);         
        setContentView(R.layout.activity_main);          
        
        InputStream inputStream = this.getClassLoader().getResourceAsStream("beautiful.xml"); 
        
        try { 
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(inputStream); 
        
            // 获取根节点    
            Element root = document.getDocumentElement();
            parse(root);
            for (Beauty b : beautyList) {
                result += b.toString();
            }         
        
            TextView textView = (TextView) findViewById(R.id.textView);         
            textView.setText(result);         
        }catch (Exception e) {
            e.printStackTrace();    
        } 
     }            
        /*
        * @param element 将要进行遍历的节点        
        */
        
    private void parse(Element element) { 
        
        NodeList nodelist = element.getChildNodes();
        int size = nodelist.getLength(); 
        
        for (int i = 0; i < size; i++) {        
            // 获取特定位置的node
            Node element2 = (Node) nodelist.item(i);         
        
            String tagName = element2.getNodeName();         
            if (tagName.equals("beauty") && element2.getNodeType() == Document.ELEMENT_NODE){ 
                 beauty = new Beauty();
                 Log.i("通知:", "创建beauty");
                 if (element2.getNodeType() == Document.ELEMENT_NODE) {
                     parse((Element) element2);
                }
                 Log.i("通知:", "添加beauty");
                 beautyList.add(beauty);    
            } 
            
            if (tagName.equals("name")) {
                String name = element2.getTextContent();
                Log.i("通知:", "name" + name);
                beauty.setName(name);    
            } 
            
            if (tagName.equals("age")) {
                String age = element2.getTextContent();
                Log.i("通知:", "age" + age);
                beauty.setAge(age);     
            } 
        } 
    }      
        
        /**          
        * @author chenzheng_java 这里使用内部类是为了效率考虑,内部类要比单独顶一个bean类更加的高效以及节约空间     */
        
        private class Beauty { 
            String name;String age;
            
            public String getName() {
                return name;
            }             
            public void setName(String name) { 
                this.name = name; 
            }             
            public String getAge() { 
                return age; 
            }         
            public void setAge(String age) { 
                this.age = age; 
            }             
            @Override
            public String toString() {
                return "美女资料 [年龄=" + age + ", 姓名=" + name + "]";
            }    
        } 
  }

 

以上是关于dom解析xml的主要内容,如果未能解决你的问题,请参考以下文章

Android之DOM解析XML

Android之DOM解析XML

在java中解析xml有哪几种方法

java代码用dom4j解析xml文件的简单操作

java解析xml的几种方式哪种最好?

怎么用java解析xml中entity