1

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

1 Answers1

0

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">.

  • Thank you ! It works! Is there a way to output the first 10 entries out 20 ? For instance my xml has 20 employees and I only need the first 10 that meets the criteria of age>30. – user3626136 Jan 21 '15 at 12:28
  • Create a counter attribute inside the Handler that increments every time you find an employee with age > 30 and test it before printing to the console, i.e.: `if(age > 30) { counter++; if(counter <= 10) { System.out.println("Name:" + currentNameValue+", Age:" + age); } }` – Bruno Marco Visioli Jan 21 '15 at 12:33
  • As I can see my output is from this string:: String xml = "CompanyNameemployee name033testermaleemployee name118testerfemaleemployee name238developermale"; Is there anyway to replace this string and actually put my file with my 20 entries ? – user3626136 Jan 21 '15 at 14:21
  • For how to read text files with Java: http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file – Bruno Marco Visioli Jan 21 '15 at 14:27
  • I know how to read from a file..but in my case this won't be effective: SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); saxParser.parse(new InputSource(new StringReader(mystring)), new Handler()); And if i try to convert my file intro a string..it won't compile – user3626136 Jan 21 '15 at 14:31
  • You can use this way `saxParser.parse("/usr/file.xml", new Handler())`. – Bruno Marco Visioli Jan 21 '15 at 16:05