Search code examples
javaxmldomsax

how to parse some specific nodes of xml using sax or dom based on given condition


i am new to jaxp and i am stuck with this i have to parse this xml using either sax or dom. i have to ask the user to enter empoyee code and then display the other nodes of that particular employee for ex:if user enters 101 then i will show

Ename:akshay
ecode:101
dp no.:10
mgr code=201

i tried alot but no results,can somebody help.

   <employees>
    <employee>
        <Ename>akshay</Ename>
        <Ecode>101</Ecode>
        <EmpSal>2100.0</EmpSal>
        <Department_code>10</Department_code>
        <Manager_code>201</Manager_code>
    </employee>
    <employee>
        <Ename>rahul</Ename>
        <Ecode>102</Ecode>
        <EmpSal>21000.0</EmpSal>
        <Department_code>20</Department_code>
        <Manager_code>202</Manager_code>
    </employee>
</employees>

the code i tried to parse this xml

public class ParseUsingDom {
    public static void main(String[] args) {

        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse("emp.xml");
            NodeList list = document.getElementsByTagName("*");
            int count = 0;
            {
            for (int i = 0; i < list.getLength(); i++) {
                Element element = (Element) list.item(i);
                String nodeName = element.getNodeName();

                if (nodeName.equals("employee")) {
                    count++;
                    System.out.println("Employee :" + count);
                } else if (nodeName.equals("Ename")) {
                    System.out.println("\tEname:\t"
                            + element.getChildNodes().item(0).getNodeValue());
                } else if (nodeName.equals("Ecode")) {
                    System.out.println("\tECode:\t"
                            + element.getChildNodes().item(0).getNodeValue());
                } else if (nodeName.equals("EmpSal")) {
                    System.out.println("\tEmpsal:\t"
                            + element.getChildNodes().item(0).getNodeValue());
                } else if (nodeName.equals("Department_code")) {
                    System.out.println("\tDepartment_code: "
                            + element.getChildNodes().item(0).getNodeValue());
                } else if (nodeName.equals("Manager_code")) {
                    System.out.println("\tManager code:\t"
                            + element.getChildNodes().item(0).getNodeValue());
                }
            }
        }} catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

}


Solution

  • This is pretty straight forward.

    1. Parse the XML as a Document with a DocumentBuilder.
    2. Ask the user for an ID. If this is command line, then a Scanner might help.
    3. Select the relevant data from the document. XPath is probably a good start.

    Take these steps individually and figure out how to do them. Put it together and you've got your solution.