android中的解析器

时间:2013-01-22 08:01:02

标签: android xml-parsing

以下是我的xml格式。我需要在Group标记下提取数据,此组标记位于common标记下。在这个常见标记中我们有4个组,我需要那个4 group数据。但是在我的xml中还有一些其他的组标签,所以数据也在提取。但是我需要公共标签中的组数据。

我的xml:

<root>
<Budget>100000.0</Budget>
<Procost>
<Costid>1</Costid>
<Costname>Fixed Cost</Costname>
<Amount>70666.66</Amount>
</Procost>
<Procostcount>1</Procostcount>
<Totalcost>70666.66</Totalcost>
<Common>
<Group>
<Empid>370</Empid>
<Reportid>-1</Reportid>
<Empcost>0</Empcost>
<Isanyreporting>Y</Isanyreporting>
<Reportcount>9</Reportcount>
<Reportname>NA</Reportname>
<Name>Shalini</Name>
<Desigid>0</Desigid>
<Gender>female</Gender>
<Yearexperience>11</Yearexperience>
<Monthexperience>0</Monthexperience>
<Phone>974113524</Phone>
<Emailid>shalini@ohile.com</Emailid>
<Projectid>1</Projectid>
<Projectname>Sabre</Projectname>
<Displaylevel>0</Displaylevel>
<Photo>None</Photo>
</Group>
<Group>
<Empid>381</Empid>
<Reportid>370</Reportid>
<Empcost>0</Empcost>
<Reportcount>0</Reportcount>
<Isanyreporting>No</Isanyreporting>
<Reportname>Shalini</Reportname>
<Name>Arun</Name>
<Desigid>1</Desigid>
<Gender>male</Gender>
<Yearexperience>8</Yearexperience>
<Monthexperience>0</Monthexperience>
<Phone>2147483647</Phone>
<Emailid>arun@ohile.com</Emailid>
<Projectid>1</Projectid>
<Projectname>Sabre</Projectname>
<Displaylevel>0</Displaylevel>
<Photo>None</Photo>
</Group>
<Group>
<Empid>382</Empid>
<Reportid>370</Reportid>
<Empcost>0</Empcost>
<Reportcount>0</Reportcount>
<Isanyreporting>No</Isanyreporting>
<Reportname>Shalini</Reportname>
<Name>Manu</Name>
<Desigid>1</Desigid>
<Gender>male</Gender>
<Yearexperience>8</Yearexperience>
<Monthexperience>0</Monthexperience>
<Phone>123487509</Phone>
<Emailid>manu@ohile.com</Emailid>
<Projectid>1</Projectid>
<Projectname>Sabre</Projectname>
<Displaylevel>0</Displaylevel>
<Photo>None</Photo>
</Group>
<Group>
<Empid>383</Empid>
<Reportid>370</Reportid>
<Empcost>0</Empcost>
<Reportcount>0</Reportcount>
<Isanyreporting>No</Isanyreporting>
<Reportname>Shalini</Reportname>
<Name>Ramakrishnan Nair</Name>
<Desigid>1</Desigid>
<Gender>male</Gender>
<Yearexperience>8</Yearexperience>
<Monthexperience>0</Monthexperience>
<Phone>467983245</Phone>
<Emailid>ramakrishnan@ohile.com</Emailid>
<Projectid>1</Projectid>
<Projectname>Sabre</Projectname>
<Displaylevel>0</Displaylevel>
<Photo>None</Photo>
</Group>
</Common>
<Project>
<Empid>371</Empid>
<Reportid>370</Reportid>
<Displaylevel>2</Displaylevel>
<Empcost>35333.3</Empcost>
<Name>Remya</Name>
<Desigid>5</Desigid>
<Gender>female</Gender>
<Yearexperience>2</Yearexperience>
<Monthexperience>0</Monthexperience>
<Phone>975443452</Phone>
<Emailid>remya@ohile.com</Emailid>
<Projectid>1</Projectid>
<Projectname>Sabre</Projectname>
<Photo>None</Photo>
<Reportcount>0</Reportcount>
<Isanyreporting>No</Isanyreporting>
<Reportname>Shalini</Reportname>
</Project>
<Project>
<Empid>374</Empid>
<Reportid>370</Reportid>
<Displaylevel>2</Displaylevel>
<Empcost>35333.3</Empcost>
<Name>Nijo Susan Thomas</Name>
<Desigid>5</Desigid>
<Gender>female</Gender>
<Yearexperience>1</Yearexperience>
<Monthexperience>1</Monthexperience>
<Phone>2147483647</Phone>
<Emailid>nijo@ohile.com</Emailid>
<Projectid>1</Projectid>
<Projectname>Sabre</Projectname>
<Photo>None</Photo>
<Reportcount>0</Reportcount>
<Isanyreporting>No</Isanyreporting>
<Reportname>Shalini</Reportname>
</Project>
<Count>6</Count>
<Procount>2</Procount>
<Fieldnames>
<Fieldnamecount>16</Fieldnamecount>
<Group>
<Fieldname>Name</Fieldname>
</Group>
<Group>
<Fieldname>Empid</Fieldname>
</Group>
<Group>
<Fieldname>Desigid</Fieldname>
</Group>
<Group>
<Fieldname>Gender</Fieldname>
</Group>
<Group>
<Fieldname>Yearexperience</Fieldname>
</Group>
<Group>
<Fieldname>Monthexperience</Fieldname>
</Group>
<Group>
<Fieldname>Phone</Fieldname>
</Group>
<Group>
<Fieldname>Emailid</Fieldname>
</Group>
<Group>
<Fieldname>Projectid</Fieldname>
</Group>
<Group>
<Fieldname>Reportid</Fieldname>
</Group>
<Group>
<Fieldname>Isanyreporting</Fieldname>
</Group>
<Group>
<Fieldname>Reportcount</Fieldname>
</Group>
<Group>
<Fieldname>Reportname</Fieldname>
</Group>
<Group>
<Fieldname>Photo</Fieldname>
</Group>
<Group>
<Fieldname>Displaylevel</Fieldname>
</Group>
<Group>
<Fieldname>Empcost</Fieldname>
</Group>
</Fieldnames>
</root>

我的代码:

public class XMLParsingDomExample extends Activity {
    List myEmpls;
    Document dom;

    public XMLParsingDomExample() {


        myEmpls = new ArrayList();
        parseXmlFile();

        parseDocument();

        printData();


    }

    /*public void runExample() {

    }*/

    private void parseXmlFile() {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        System.out.println("Inside xml");

        try {
            URL url = new URL(
                    "http://fhkghkkl.guio");
            // "http://totheriver.com/learn/xml/code/employees.xml");

            DocumentBuilder db = dbf.newDocumentBuilder();

            dom = db.parse(new InputSource(url.openStream()));

            dom.getDocumentElement().normalize();
            System.out.println("after xml");

        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (SAXException se) {
            se.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }


    private void parseDocument() {
        Element docEle = dom.getDocumentElement();
        NodeList nl = docEle.getElementsByTagName("Group");
        // NodeList nl = docEle.getElementsByTagName("Common");
        // NodeList nl = dom.getDocumentElement().getChildNodes();

        System.out.println("nodelist is :" + nl);
        if (nl != null && nl.getLength() > 0) {
            for (int i = 0; i < nl.getLength(); i++) {

                Element el = (Element) nl.item(i);

                System.out.println("b4 getemployee");
                Employee e = getEmployee(el);

                myEmpls.add(e);
                System.out.println("employee size is =" + myEmpls.size());
            }
        }
    }

    private Employee getEmployee(Element empE1) {

        String empId = getTextValue(empE1, "Empid");

        String name = getTextValue(empE1, "Name");
        String repCount = getTextValue(empE1, "Reportcount");
        String desid = getTextValue(empE1, "Desigid");
        String projectid = getTextValue(empE1, "Projectid");
        String projectname = getTextValue(empE1, "Projectname");
        // String gender =
        // getTextValue(empE1, "Gender");
        // int yrEx = getIntValue(empE1,
        // "Yearexperience");
        // int mnthEx = getIntValue(empE1, "Monthexperience");
        // int prcId = getIntValue(empE1, "ProjectId"); String prcname =
        // getTextValue(empE1,
        // "Projectname");
        // int dislev = getIntValue(empE1, "Displaylevel"); String photo
        // = getTextValue(empE1, "Photo");
        // int phone = getIntValue(empE1, "Phone");
        // String emailid = getTextValue(empE1, "Emailid");

        Employee e = new Employee(empId, name, repCount, desid, projectid,
                projectname);
        return e;
    }

    // }

    private String getTextValue(Element ele, String tagName) {
        String textVal = null;
        NodeList nl = ele.getElementsByTagName(tagName);
        if (nl != null && nl.getLength() > 0) {
            Element el = (Element) nl.item(0);
            textVal = el.getFirstChild().getNodeValue();
        }

        return textVal;
    }

    private int getIntValue(Element ele, String tagName) {
        // in production application you would catch the exception
        return Integer.parseInt(getTextValue(ele, tagName));
    }

    private void printData() {

        System.out.println("No of Employees '" + myEmpls.size() + "'.");

        Iterator it = myEmpls.iterator();
        while (it.hasNext()) {
            System.out.println(it.next().toString());
        }
    }
}
    /*public static void main(String[] args) {

        XMLParsingDomExample dpe = new XMLParsingDomExample();

        dpe.runExample();
    }
}*/

我的logcat:

我一个人需要这个:

01-22 13:10:44.519: I/System.out(1606): Employee Details - EmployeeId:370.Empname :Shalini, DesignId:9, ReportCount:0, ProjectId:1, ProjectName:Sabre, 
01-22 13:10:44.537: I/System.out(1606): Employee Details - EmployeeId:381.Empname :Arun, DesignId:0, ReportCount:1, ProjectId:1, ProjectName:Sabre, 
01-22 13:10:44.537: I/System.out(1606): Employee Details - EmployeeId:382.Empname :Manu, DesignId:0, ReportCount:1, ProjectId:1, ProjectName:Sabre, 
01-22 13:10:44.547: I/System.out(1606): Employee Details - EmployeeId:383.Empname :Ramakrishnan Nair, DesignId:0, ReportCount:1, ProjectId:1, ProjectName:Sabre, 
01-22 13:10:44.547: I/System.out(1606): 

但这也将到来:

Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null, 
    01-22 13:10:44.578: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null, 
    01-22 13:10:44.578: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null, 
    01-22 13:10:44.597: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null, 
    01-22 13:10:44.597: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null, 
    01-22 13:10:44.617: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null, 
    01-22 13:10:44.617: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null, 
    01-22 13:10:44.637: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null, 
    01-22 13:10:44.637: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null, 
    01-22 13:10:44.657: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null, 
    01-22 13:10:44.657: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null, 
    01-22 13:10:44.667: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null, 
    01-22 13:10:44.677: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null, 
    01-22 13:10:44.677: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null, 
    01-22 13:10:44.687: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null, 
    01-22 13:10:44.698: I/System.out(1606): Employee Details - EmployeeId:null.Empname :null, DesignId:null, ReportCount:null, ProjectId:null, ProjectName:null,

1 个答案:

答案 0 :(得分:1)

如果您确定,只有一个Common元素,并且其所有子元素都是Group个,而不是

NodeList common = docEle.getElementsByTagName("Common");
NodeList nl = common.item(0).getChildNodes();

如果没有,请使用xpath更通用的解决方案:

XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
NodeList nl = (NodeList) xpath.evaluate("Common/Group", docEle, XPathConstants.NODESET);