SGN
SGN

Reputation: 341

Conditional XML Parsing using Java

I am looking for a suitable parser to parse through the given XML. I want to parse through the full XML only when tag - 'employee', attribute - 'validated=false' else stop parsing. How we can perform this conditional XML parsing using SAX, STAX or any other parsers ?

<?xml version="1.0"?>
<database>
<employee validated="False">
  <name>Lars </name>
  <street validated="False"> Test </street>
  <telephone number= "0123"/>
</employee>  
<employee validated="True">
  <name>Baegs </name>
  <street validated="True"> Test </street>
  <telephone number= "0123"/>
</employee>  
</database>

I have tried the below SAX parser code

    List<XmlObjects> xmlObjects;
    String espXmlFileName;
    String tmpValue;
    XmlObjects xmlObjectsTmp;

    public SaxParser(String espXmlFileName) {
        this.espXmlFileName = espXmlFileName;
        xmlObjects = new ArrayList<XmlObjects>();
        parseDocument();
        printDatas();
    }

    private void printDatas() {
        for (XmlObjects tmpB : xmlObjects) {
            System.out.println(tmpB.toString());
        }

    }

    private void parseDocument() {
        SAXParserFactory factory = SAXParserFactory.newInstance();

        try {

            SAXParser parser = factory.newSAXParser();

            parser.parse(espXmlFileName, this);

        } catch (ParserConfigurationException e) {

            System.out.println("ParserConfig error");

        } catch (SAXException e) {

            System.out.println("SAXException : xml not well formed");

        } catch (IOException e) {

            System.out.println("IO error");

        }

    }


public void startElement(String uri, String localName, String qName,
            org.xml.sax.Attributes attributes) throws SAXException {
        if (qName.equalsIgnoreCase("employee")) {

            String value1 = attributes.getValue("validated");
            if (value1.equalsIgnoreCase("FALSE")) {
                if (qName.equalsIgnoreCase("name")) {
                    String value2 = attributes.getValue("validated");
                        xmlObjectsTmp.setName(attributes
                                            .getValue("name"));
                                }

                            }


    public void endElement(String uri, String localName, String qName)
            throws SAXException {

        if (qName.equalsIgnoreCase("employee")) {
            xmlObjects.add(xmlObjectsTmp);
        }

        if (qName.equalsIgnoreCase("name")) {
            xmlObjectsTmp.setName(tmpValue);
        }


    }

    public static void main(String argv[]) {

        new SaxParser("C:\\xml\\xml2.xml");

    }

Upvotes: 0

Views: 1368

Answers (1)

adamretter
adamretter

Reputation: 3517

In the startElement method of your ContentHandler you can simply throw a SAXException to abort parsing when your validated attribute has the value True.

For example:

@Override
public void startElement(final String uri, final String localName,
    final String qName, final Attributes attributes) throws SAXException {

    if(localName.equalsIgnoreCase("employee") || localName.equalsIgnoreCase("street")) {
        final String validated = attributes.getValue("validated");
        if(validated != null && !validated.equals("False")) {
            throw new SAXException(localName + " has already been validated");
        } else {
            //your processing logic here
        }
    }
}

You can register an ErrorHandler to deal with the error in your own way if you wish.

Upvotes: 1

Related Questions