简体   繁体   中英

Java Sax XML Parser, parsing custom “values” within XML tags?

I haven't worked much with XML before so maybe my ignorance on proper terminology is hurting me in my searches on how to do this. I have the code snippet below which I am using to parse an XML file like the one below. The problem is that it only picks up XML values within <Tag>Value</Tag> but not for the one below where I need to get the value of TagValue , which in this case would be "Russell Diamond" .

I would appreciate if anyone can offer assistance as to how to get custom values like this. Thanks.

<Tag TagName="#Subject" TagDataType="Text" TagValue="Russell Diamond"/>

The snippet I am using:

public void printElementNames(String fileName) throws IOException {
    //test write to file
       FileWriter fstream = new FileWriter("/home/user/Desktop/readEDRMtest.txt");
        final BufferedWriter out = new BufferedWriter(fstream);


    //

    try {
        SAXParserFactory parserFact = SAXParserFactory.newInstance();
        SAXParser parser = parserFact.newSAXParser();
        System.out.println("XML Elements: ");
        DefaultHandler handler = new DefaultHandler() {
            public void startElement(String uri, String lName, String ele,
                    Attributes attributes) throws SAXException {
                // print elements of xml
                System.out.println(ele);
                try {
                    out.write(ele);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

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


                System.out.println("Value : "
                    + new String(ch, start, length));
                try {
                    out.write("Value : "
                            + new String(ch, start, length));
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

        };

You want to look into extracting attributes . Search on that and you'll find your answer.

The DefaultHandler class's startElement(...) method passes a parameter called attributes that refers to an Attribute object. The API for the Attribute interface will describe how to extract the information you need from this object.

For example:

out.write(attributes.getValue("TagValue"));

This is a stripped down and working version of your code snippet:

import java.io.File;
import java.io.IOException;

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 SAX 
{
    public static void main(String[] args) throws IOException {
        new SAX().printElementNames("Delete.xml");
    }

    public void printElementNames(String fileName) throws IOException 
    {

        try {
            SAXParserFactory parserFact = SAXParserFactory.newInstance();
            SAXParser parser = parserFact.newSAXParser();
            DefaultHandler handler = new DefaultHandler() 
            {
                public void startElement(String uri, String lName, String ele,  Attributes attributes) throws SAXException {
                    System.out.println(ele);
                    System.out.println(attributes.getValue("TagValue"));
                }

                public void characters(char ch[], int start, int length) throws SAXException {
                    System.out.println("Value : " + new String(ch, start, length));
                }               
            };

            parser.parse(new File(fileName), handler);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

Content of Delete.xml

<Tag TagName="#Subject" TagDataType="Text" TagValue="Russell Diamond"/>

Further reading:

http://www.java-samples.com/showtutorial.php?tutorialid=152

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM