加入 SAX 检索到的两个字符串

Join two strings retrieved by SAX

我有一个像这样的 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的一些子节点,而不是全部,将它们放在一个字符串变量中并显示它们。

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

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 值,而不会 return 任何关于作者全名的信息。

我有另一个 class 用于解析 ArticleTitle 节点,它们都在 Main class.

中调用

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

每次调用characters方法时,fullName变量都会被覆盖。我认为您应该将该变量移出到处理程序中:当 Author 开始时使用空字符串初始化,并在它结束时写出。串联应该像您一样工作。我还没有尝试过这个,但类似的东西应该有用:

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

}