Java Sax XML 解析器,解析自定义“值”在 XML 标签内?

发布于 2024-10-18 23:27:03 字数 1839 浏览 1 评论 0原文

我以前没有太多使用过 XML,所以也许我对正确术语的无知在我搜索如何做到这一点时受到了伤害。我有下面的代码片段,我用它来解析如下所示的 XML 文件。问题是它只获取 Value 中的 XML 值,但不获取下面我需要获取 TagValue 值的 XML 值,在本例中为“Russell Diamond”。

如果有人可以提供有关如何获取这样的自定义值的帮助,我将不胜感激。谢谢。

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

我正在使用的片段:

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

            }

        };

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

            }

        };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放手` 2024-10-25 23:27:03

您想要研究提取属性。搜索一下,你就会找到答案。

DefaultHandler 类的 startElement(...) 方法传递一个称为 attribute 的参数,该参数引用 Attribute 对象。 属性接口的 API 将描述如何从该对象中提取所需的信息。

例如:

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

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"));
往事随风而去 2024-10-25 23:27:03

这是代码片段的精简版和工作版本:

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 的内容

;

进一步阅读:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文