Search code examples
javaxmlsax

Read XML File with condition age >30 and output in Java Console


I'm reading a XML File in Eclipse and my Output is in my Console. So far I managed to output my entries. But I need to print the entries where my employees are over 30 year old.

This is my XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<company>
    <name>CompanyName</name>
    <employees id="0">
        <name>employee name0</name>
        <age>33</age>
        <role>tester</role>
        <gen>male</gen>
    </employees>
    <employees id="1">
        <name>employee name1</name>
        <age>18</age>
        <role>tester</role>
        <gen>female</gen>
    </employees>
    <employees id="2">
        <name>employee name2</name>
        <age>38</age>
        <role>developer</role>
        <gen>male</gen>
    </employees>
    </company>

And this is what I have been trying :

if (qName.equals("age"))
{
  int age2;
  String age=attributes.getValue("age");
   age2=Integer.ParseInt(age)
if (age2>30){
System.out.println("\tAge="+age2);
}

So I want print down

employee with id=0 and employee with id=2 because they have age >30


Solution

  • Considering that you're using SAXParser and that this snippet of code is inside the overridden method startElement you'll need to override the characters and endElement too. Something like this:

    class Handler extends DefaultHandler {
    
    String currentElement;
    String currentAgeValue;
    String currentNameValue;
    
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        super.startElement(uri, localName, qName, attributes);
        currentElement = qName;
    }
    
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        super.characters(ch, start, length);
        switch(currentElement) {
            case "age":
            currentAgeValue = new String(ch, start, length);
            break;
            case "name":
            currentNameValue = new String(ch, start, length);
            break;
        }
    }
    
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        super.endElement(uri, localName, qName);
        if(qName.equals("employees")) {
            int age = Integer.parseInt(currentAgeValue);
            if(age > 30) { 
                System.out.println("Name:" + currentNameValue+", Age:" + age);
            }
        }
    }
    
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
        String xml = "<company><name>CompanyName</name><employees id=\"0\"><name>employee name0</name><age>33</age><role>tester</role><gen>male</gen></employees><employees id=\"1\"><name>employee name1</name><age>18</age><role>tester</role><gen>female</gen></employees><employees id=\"2\"><name>employee name2</name><age>38</age><role>developer</role><gen>male</gen></employees></company>";
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        saxParser.parse(new InputSource(new StringReader(xml)), new Handler());
    }
    

    The output will be:

    Name:employee name0, Age:33
    Name:employee name2, Age:38
    

    The characters method is called when reading the values of a given element, the Attribute parameter of startElement keeps value for XML attributes like id in <employees id="2">.