Java Sax XML Parser parsing custom "values" in XML tags?

I didn’t work very much with XML, so perhaps my ignorance of the correct terminology hurts me in my search for how to do this. I have a piece of code below which I use to parse an XML file similar to the one below. The problem is that it only accepts XML values ​​in <Tag>Value</Tag>, but not for below, where I need to get the value TagValue, which in this case will be "Russell Diamond".

I would appreciate if anyone could offer help on how to get custom values ​​like this. Thank.

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

Used fragment:

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();
                }

            }

        };
+3
source share
2 answers

You want to learn attribute extraction . , .

startElement (...) DefaultHandler , , . API , .

:

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

:

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();
        }
    }
}

Delete.xml

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

:

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

+2

Source: https://habr.com/ru/post/1793774/


All Articles