加入SAX检索的两个字符串

时间:2015-01-24 13:59:27

标签: java xml sax saxparser

我有一个像这样的XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<Article>
    <ArticleTitle>Java-SAX Tutorial</ArticleTitle>
    <Author>
        <FamilyName>Yong</FamilyName>
        <GivenName>Mook</GivenName>
        <GivenName>Kim</GivenName>
        <nickname>mkyong</nickname>
        <salary>100000</salary>
    </Author>
    <Author>
        <FamilyName>Low</FamilyName>
        <GivenName>Yin</GivenName>
        <GivenName>Fong</GivenName>
        <nickname>fong fong</nickname>
        <salary>200000</salary>
    </Author>
</Article>

我在mkyong的教程here中尝试过这个例子,我可以使用SAX从中完美地检索数据,它给了我:

Article Title : Java-SAX Tutorial
Given Name : Kim
Given Name : Mook
Family Name : Yong
Given Name : Yin
Given Name : Fong
Family Name : Low

但是我想让它给我这样的东西:

Article Title : Java-SAX Tutorial
Author : Kim Mook Yong
Author : Yin Fong Low

换句话说,我想检索节点Author的一些子节点,而不是所有子节点,将它们放在一个字符串变量中并显示它们。

这是我用来解析作者的类,我试图做的修改:

public class ReadAuthors {

    public void parse(String filePath) {

        try {

            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();

            DefaultHandler handler = new DefaultHandler() {

                boolean bFamilyName = false;
                boolean bGivenName = false;

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

                    if (qName.equalsIgnoreCase("FamilyName")) {
                        bFamilyName = true;
                    }

                    if (qName.equalsIgnoreCase("GivenName")) {
                        bGivenName = true;
                    }

            }

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

            }

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

                String fullName = "";

                String familyName = "";
                String givenName ="";

                if (bFamilyName) {
                    familyName = new String(ch, start, length);
                    fullName += familyName;
                    bFamilyName = false;
                }

                if (bGivenName) {
                    givenName = new String(ch, start, length);
                    fullName += " " + givenName;
                    bGivenName = false;
                }

                System.out.println("Full Name : " + fullName);

            }
             };

             saxParser.parse(filePath, handler);

        } catch (Exception e) {
               e.printStackTrace();
        }
   }

}

通过此修改,它仅向我提供ArticleTitle值,并且不会返回有关作者全名的任何内容。

我有另一个用于解析ArticleTitle节点的类,它们都在Main类中调用。

我做错了什么?我该如何解决?

1 个答案:

答案 0 :(得分:1)

每次调用字符方法时都会覆盖fullName变量。我认为你应该将该变量移出到处理程序中:init在作者启动时使用空字符串,在结束时写出。连接应该像你一样工作。我还没有尝试过这个,但是similear应该可以工作:

public class ReadAuthors {

    public void parse(String filePath) {

        try {

            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();

            DefaultHandler handler = new DefaultHandler() {

                boolean bName = false;
                String fullName = "";

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

                    if (qName.equalsIgnoreCase("FamilyName")) {
                        bName = true;
                    }

                    if (qName.equalsIgnoreCase("GivenName")) {
                        bName = true;
                    }

                    if (qName.equalsIgnoreCase("Author")) {
                        fullName = "";
                    }

            }

            @Override
            public void endElement(String uri, String localName,
                String qName) throws SAXException {
                    if (qName.equalsIgnoreCase("Author")) {
                        System.out.println("Full Name : " + fullName);
                    }
            }

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

                String name = "";

                if (bName) {
                    name = new String(ch, start, length);
                    fullName += name;
                    bName = false;
                }

            }
             };

             saxParser.parse(filePath, handler);

        } catch (Exception e) {
               e.printStackTrace();
        }
   }

}