user1882812
user1882812

Reputation: 946

XML Read one special element out of different xml files using java saxparser

i have many different xml documents with different strucutre but they all have one element that i want to read. that element is

<text>

for example i have this strucutre

<foo>
    <description></description>
    <date></date>
    <link></link>
        <item>
            <description></description>
            <text></text> // THIS IS WHAT I NEED
        </item>
</foo>

How can i get those elements out of every xml file independent to its strucutre using a java sax parser?

Upvotes: 0

Views: 168

Answers (1)

Zaw Than oo
Zaw Than oo

Reputation: 9935

If you have specific tag name(Eg, text), you only need to check and retrieve the value of the tag in XML Reader. Add any.xml into the class-path. Try as below.

XMLReader.java

import java.io.CharArrayWriter;
import java.io.FileInputStream;
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 XMLReader {
    private Reader reader = new Reader();
    private class Reader extends DefaultHandler {
        private List<String> textList = new ArrayList<String>();
        private CharArrayWriter text;

        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            text = new CharArrayWriter();
        }

        public void endElement(String uri, String localName, String qName) throws SAXException {
            if(qName.equalsIgnoreCase("text")) {
                textList.add(text.toString().trim());
            }
        }

        public void characters(char[] ch, int start, int length) throws SAXException {
            text.write(ch, start, length);
        }

        public List<String> getTextList() {
            return textList;
        }
    }
    public List<String> getTextList() {
        return reader.getTextList();
    }

    public void load() {
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            saxParser.parse(new FileInputStream("any.xml"), reader);
        } catch (Exception e) {
            e.printStackTrace();
        }       
    }
    public static void main(String[] args) {
        XMLReader reader = new XMLReader();
        reader.load();
        List<String> textList = reader.getTextList();
        for(String text : textList) {
            System.out.println(text);
        }
    }
}

any.xml

<?xml version="1.0" encoding="UTF-8"?>
<foo>
    <description></description>
    <date></date>
    <link></link>
    <item>
        <description></description>
        <text>xxxx</text>
    </item>
    <text>yyyyy</text>
    <any>
        <text>zzzzz</text>      
    </any>
</foo>

Upvotes: 1

Related Questions