Search code examples
javaxmlxml-parsingsax

Parsing specific XML file


I have a problem with parsing XML file. It structure looks like:

    <?xml version="1.0" encoding="UTF-8"?>
<teryt>
<catalog name="TERC" type="all" date="2014-01-01">
<row>
<col name="WOJ">02</col>
<col name="POW"/>
<col name="GMI"/>
<col name="RODZ"/>
<col name="NAZWA">DOLNOŚLĄSKIE</col>
<col name="NAZDOD">województwo</col>
<col name="STAN_NA">2014-01-01</col>
</row>
<row>
<col name="WOJ">02</col>
<col name="POW">01</col>
<col name="GMI"/>
<col name="RODZ"/>
<col name="NAZWA">bolesławiecki</col>
<col name="NAZDOD">powiat</col>
<col name="STAN_NA">2014-01-01</col>
</row>
...
</catalog
</teryt>

I have parser right now but it only read file to the one variable. My parser code.

package pl.op.web.common;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.primefaces.event.FileUploadEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.io.IOException;
import java.io.InputStream;

@Name("op.xmlParser")
@Scope(ScopeType.SESSION)
public class XMLParser extends DefaultHandler {

    private Logger log = LoggerFactory.getLogger(XMLParser.class);

    private InputStream uploadedAreaFile;

    public void uploadAreaXML(FileUploadEvent event) {
        try {
            uploadedAreaFile = event.getFile().getInputstream();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
        public void getAreaXml() {
            try {
                SAXParserFactory saxParserFactory = SAXParserFactory
                        .newInstance();
                SAXParser saxParser = saxParserFactory.newSAXParser();

                DefaultHandler defaultHandler = new DefaultHandler() {

                    Boolean wojTag = false;
                    Boolean powTag = false;
                    Boolean gmiTag = false;
                    Boolean rodzTag = false;
                    Boolean nazwaTag = false;
                    Boolean nazdodTag = false;
                    Boolean stan_naTag = false;

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

                        if (qName.equalsIgnoreCase("col")) {
                            wojTag = true;
                        }
                        if (qName.equalsIgnoreCase("col")) {
                            powTag = true;
                        }
                        if (qName.equalsIgnoreCase("col")) {
                            gmiTag = true;
                        }
                        if (qName.equalsIgnoreCase("col")) {
                            rodzTag = true;
                        }
                        if (qName.equalsIgnoreCase("col")) {
                            nazwaTag = true;
                        }
                        if (qName.equalsIgnoreCase("col")) {
                            nazdodTag = true;
                        }
                        if (qName.equalsIgnoreCase("col")) {
                            stan_naTag = true;
                        }
                    }

                    public void characters(char ch[], int start, int length)
                            throws SAXException {

                        if (wojTag.equals(true)) {
                            log.info("Woj : "
                                    + new String(ch, start, length));
                        }
                        if (powTag.equals(true)) {
                            log.info("Pow : "
                                    + new String(ch, start, length));
                        }
                        if (gmiTag.equals(true)) {
                            log.info("Gmi : "
                                    + new String(ch, start, length));
                        }
                        if (rodzTag.equals(true)) {
                            log.info("Rodz : "
                                    + new String(ch, start, length));
                        }
                        if (nazwaTag.equals(true)) {
                            log.info("Nazwa : "
                                    + new String(ch, start, length));
                        }
                        if (nazdodTag.equals(true)) {
                            log.info("Nazdod : "
                                    + new String(ch, start, length));
                        }
                        if (stan_naTag.equals(true)) {
                            log.info("Stan_na : "
                                    + new String(ch, start, length));
                        }
                    }

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

                        if (qName.equalsIgnoreCase("col")) {
                            wojTag = false;
                        }
                        if (qName.equalsIgnoreCase("col")) {
                            powTag = false;
                        }
                        if (qName.equalsIgnoreCase("col")) {
                            gmiTag = false;
                        }
                        if (qName.equalsIgnoreCase("col")) {
                            rodzTag = false;
                        }
                        if (qName.equalsIgnoreCase("col")) {
                            nazwaTag = false;
                        }
                        if (qName.equalsIgnoreCase("col")) {
                            nazdodTag = false;
                        }
                        if (qName.equalsIgnoreCase("col")) {
                            stan_naTag = false;
                        }
                    }
                };

            saxParser.parse(uploadedAreaFile, defaultHandler);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}    

How can I have every single attribute (WOJ, POW, NAZWA etc.) in a single variable ?


Solution

  • This is how you can do...

    package pl.op.web.common;

    import java.util.ArrayList;
    
    import java.util.List;
    
    import javax.xml.parsers.SAXParser;
    
    import javax.xml.parsers.SAXParserFactory;
    
    import org.xml.sax.Attributes;
    
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
    
    public class XMLParser extends DefaultHandler {
        public static void main(String[] args) {
            XMLParser parser = new XMLParser();
            parser.getAreaXml();
        }
    
        public void getAreaXml() {
            final List<StringBuilder> first = new ArrayList<StringBuilder>();
            try {
                SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
                SAXParser saxParser = saxParserFactory.newSAXParser();
                DefaultHandler defaultHandler = new DefaultHandler() {
                    Boolean wojTag = false;
                    Boolean powTag = false;
                    Boolean gmiTag = false;
                    Boolean rodzTag = false;
                    Boolean nazwaTag = false;
                    Boolean nazdodTag = false;
                    Boolean stan_naTag = false;
    
                    public void startElement(String uri, String localName,
                            String qName, Attributes attributes)
                            throws SAXException {
    
                        if (qName.equalsIgnoreCase("col")) {
                            if (attributes.getValue(0) != null)
                                if (attributes.getValue(0).equalsIgnoreCase("WOJ"))
                                    wojTag = true;
    
                            if (attributes.getValue(0) != null)
                                if (attributes.getValue(0).equalsIgnoreCase("POW"))
                                    powTag = true;
    
                            if (attributes.getValue(0) != null)
                                if (attributes.getValue(0).equalsIgnoreCase("GMI"))
                                    gmiTag = true;
    
                            if (attributes.getValue(0) != null)
                                if (attributes.getValue(0).equalsIgnoreCase("RODZ"))
                                    rodzTag = true;
    
                            if (attributes.getValue(0) != null)
                                if (attributes.getValue(0)
                                        .equalsIgnoreCase("NAZWA"))
                                    nazwaTag = true;
    
                            if (attributes.getValue(0) != null)
                                if (attributes.getValue(0).equalsIgnoreCase(
                                        "NAZDOD"))
                                    nazdodTag = true;
                            if (attributes.getValue(0) != null)
                                if (attributes.getValue(0).equalsIgnoreCase(
                                        "STAN_NA"))
                                    stan_naTag = true;
                        }
                    }
    
                    public void characters(char ch[], int start, int length)
                            throws SAXException {
    
                        StringBuilder temp = new StringBuilder();
    
                        if (wojTag.equals(true)) {
    
                            temp.append(new String(ch, start, length));
                            temp.append("  ");
                        }
                        if (powTag.equals(true)) {
    
                            temp.append(new String(ch, start, length));
                            temp.append("  ");
                        }
                        if (gmiTag.equals(true)) {
    
                            temp.append(new String(ch, start, length));
                            temp.append("  ");
                        }
                        if (rodzTag.equals(true)) {
    
                            temp.append(new String(ch, start, length));
                            temp.append("  ");
                        }
                        if (nazwaTag.equals(true)) {
    
                            temp.append(new String(ch, start, length));
                            temp.append("  ");
                        }
                        if (nazdodTag.equals(true)) {
    
                            temp.append(new String(ch, start, length));
                            temp.append("  ");
                        }
                        if (stan_naTag.equals(true)) {
    
                            temp.append(new String(ch, start, length));
                            temp.append("  ");
                        }
    
                        first.add(temp);
                    }
    
                    public void endElement(String uri, String localName,
                            String qName) throws SAXException {
    
                        if (qName.equalsIgnoreCase("col")) {
                            wojTag = false;
                        }
                        if (qName.equalsIgnoreCase("col")) {
                            powTag = false;
                        }
                        if (qName.equalsIgnoreCase("col")) {
                            gmiTag = false;
                        }
                        if (qName.equalsIgnoreCase("col")) {
                            rodzTag = false;
                        }
                        if (qName.equalsIgnoreCase("col")) {
                            nazwaTag = false;
                        }
                        if (qName.equalsIgnoreCase("col")) {
                            nazdodTag = false;
                        }
                        if (qName.equalsIgnoreCase("col")) {
                            stan_naTag = false;
                        }
                    }
                };
    
                saxParser.parse("E:/ResourcePDF/zippertest/src/test.xml",
                        defaultHandler);
    
                for (StringBuilder s : first) {
                    System.out.print(s);
    
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    out put will be like this for above xml file

    02 DOLNO?L?SKIE województwo 2014-01-01 02 01 boles?awiecki powiat 2014-01-01

    Am stroing data btw row tag into one StringBuilder. and adding it to "first" stringBuilder List . and then printing it.