public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        paserXml();
    }
    
    
public void paserXml() {
        
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        
try {
            
            DocumentBuilder db = dbf.newDocumentBuilder();
            
            Document doc = db.parse(getAssets().open(
"data1.xml"));
            
            Element el = doc.getDocumentElement();
            
            NodeList childNodes = el.getChildNodes();
            
            
for (
int i = 
0; i < childNodes.getLength(); i++) {
                
                Node node = childNodes.item(i);
                System.out.println(
"开始"+node.getNodeType());
                
                
if (node.getNodeType() == Node.ELEMENT_NODE) {
                    
                    Student stu = 
new Student();
                    
                    Element element = (Element) node;
                    
                    String id = element.getAttribute(
"id");
                    stu.setId(Integer.parseInt(id));
                    
                    NodeList childNodes2 = element.getChildNodes();
                    System.out.println(
"childNodes2" + childNodes2.getLength());
                    
for (
int j = 
0; j < childNodes2.getLength(); j++) {
                        Node node2 = childNodes2.item(j);
                        
                        
if (node2.getNodeType() == Node.ELEMENT_NODE) {
                            
                            Element me1 = (Element) node2;
                            
                            String value = me1.getFirstChild().getNodeValue();
                            
if (
"name".equals(me1.getNodeName())) {
                                stu.setName(value);
                            } 
else if (
"age".equals(me1.getNodeName())) {
                                stu.setAge(Integer.parseInt(value));
                            }
                        }
                        System.out.println(
"结束"+node2.getNodeType());
                    }
                    System.out.println(stu.toString());
                }
            }
        } 
catch (ParserConfigurationException e) {
            System.out.println(
"ParserConfigurationException");
            e.printStackTrace();
        } 
catch (SAXException e) {
            System.out.println(
"SAXException");
            e.printStackTrace();
        } 
catch (IOException e) {
            System.out.println(
"IOException");
            e.printStackTrace();
        }
    }
} 
javabean
 
public class Student {
    private int id;
    
private String name;
    
private int age;
    
public Student() {
        
super();
        
    }
    
public Student(
int id, String name, 
int age) {
        
super();
        
this.id = id;
        
this.name = name;
        
this.age = age;
    }
    
public int getId() {
        
return id;
    }
    
public void setId(
int id) {
        
this.id = id;
    }
    
public String 
getName() {
        
return name;
    }
    
public void setName(String name) {
        
this.name = name;
    }
    
public int getAge() {
        
return age;
    }
    
public void setAge(
int age) {
        
this.age = age;
    }
    
@Override
    public String 
toString() {
        
return "Student [id=" + id + 
", name=" + name + 
", age=" + age + 
"]";
    }
}
                
                
                
        
    
 
                    转载请注明原文地址: https://ju.6miu.com/read-1125817.html