SAXParser - 解码XML元素中的值后,仅获得字符长度126

时间:2013-05-23 06:31:12

标签: java xml-parsing sax saxparser

我正在尝试使用SAXParser从下面显示的xml中的元素“UserDetails”中获取编码字符。我只得到第一个 2行编码字符,长度为126.我无法获得所有编码字符。任何想要检索这些角色的想法。

**User.xml**

<UserStatus>FALSE</UserStatus>
<UserDetails>
**AAAAAAAA//8BAAAAAAAAAAEAAQAAAP///f///wAAAQAAAAAAAgABAAIAAQAAAAAA//8CAAAAAAABAP////8A
AAAA//8AAP////8AAAAA/////wAA//////3//f/9//7//v/9/wAA/f/8//z//P/8//3//v/+//7//P/9//3/**
/P/7//r/+//8//v/+//6//r/+//5//n//P/6//r//P/6//n/+P/6//r/+v/6//r/+v/7//z//P/5//r/+v/7
//r/+f/5//r/+v/5//r/+f/5//r/+//6//n/9//3//n/+f/5//j/9//5//n/+P/5//n/+f/6//f/+P/4//f/
+P/5//j/+f/6//f/9v/2//f/9//3//n/+v/4//n/+f/3//b/9//4//n/9//4//b/9//0//f/9v/3//b/9//2
//T/9f/5//T/9f/2//X/9P/2//b/9v/0//T/8//2//T/9P/0//X/9v/1//f/9P/0//L/8v/y//L/9v/3//b/
9v/2//f/9v/1//X/9f/1//T/9v/3//X/9f/1//b/9v/2//b/9//3//n/9//1//T/8//1//b/9v/3//n/9//1
//T/9P/0//X/9v/0//b/9v/3//b/9v/1//P/9P/1//f/9//2//n/9//3//b/9//3//f/9//4//f/+f/3//b/
9v/0//X/9v/5//n/9//6//f/9//4//f/9//4//j/9//5//n/+f/2//X/9//0//T/9f/2//j/9v/4//b/9f/1
//T/9f/1//f/9v/3//X/9v/2//b/9f/1//T/9f/5//j/+P/4//n/+P/3//b/+P/4//f/9//2//j/+f/2//b/
</UserDetails>
<UserStatus>TRUE</UserStatus>
<UserDetails>
**AAAAAAAA//8BAAAAAAAAAAEAAQAAAP///f///wAAAQAAAAAAAgABAAIAAQAAAAAA//8CAAAAAAABAP////8A
AAAA//8AAP////8AAAAA/////wAA//////3//f/9//7//v/9/wAA/f/8//z//P/8//3//v/+//7//P/9//3/**
/P/7//r/+//8//v/+//6//r/+//5//n//P/6//r//P/6//n/+P/6//r/+v/6//r/+v/7//z//P/5//r/+v/7
//r/+f/5//r/+v/5//r/+f/5//r/+//6//n/9//3//n/+f/5//j/9//5//n/+P/5//n/+f/6//f/+P/4//f/
+P/5//j/+f/6//f/9v/2//f/9//3//n/+v/4//n/+f/3//b/9//4//n/9//4//b/9//0//f/9v/3//b/9//2
//T/9f/5//T/9f/2//X/9P/2//b/9v/0//T/8//2//T/9P/0//X/9v/1//f/9P/0//L/8v/y//L/9v/3//b/
9v/2//f/9v/1//X/9f/1//T/9v/3//X/9f/1//b/9v/2//b/9//3//n/9//1//T/8//1//b/9v/3//n/9//1
//T/9P/0//X/9v/0//b/9v/3//b/9v/1//P/9P/1//f/9//2//n/9//3//b/9//3//f/9//4//f/+f/3//b/
9v/0//X/9v/5//n/9//6//f/9//4//f/9//4//j/9//5//n/+f/2//X/9//0//T/9f/2//j/9v/4//b/9f/1
//T/9f/1//f/9v/3//X/9v/2//b/9f/1//T/9f/5//j/+P/4//n/+P/3//b/+P/4//f/9//2//j/+f/2//b/
</UserDetails>



**UserDetails.java**

import java.io.File;

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 UserDetails {

    public static void main(String args[]){

    File file = new File("C:/user_records/User.xml");

    final StringBuffer curCharValue = new StringBuffer(1024);

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

        DefaultHandler handler = new DefaultHandler() {
            String sd = "";
            String createdStr = "";

            boolean stat= false;

            public void startElement(String uri, String localName,
                    String qName, Attributes attributes)
                    throws SAXException {
                curCharValue.setLength(0);
                System.out.println("Start Element :" + qName);

                if (qName.equalsIgnoreCase("UserDetails")) {
                    stat= true;
                }

            }

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

                System.out.println("End Element : " + qName);

            }

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

                if (stat) {
                    System.out.println("User Details are : "
                            + new String(ch, start, length));

                    curCharValue.append(ch, start, length);

                    byte[] decoded = Base64.decodeBase64(new String(
                            curCharValue));

                    System.out.println(decoded.length
                            + " is decoded length");
                    for (int i = 0; i < decoded.length; i++) {

                        createdStr = decoded[i] + "";
                        if (createdStr.equals("-")
                                || i == (decoded.length - 1)) {
                            sd += decoded[i];
                        } else {
                            sd += decoded[i] + ",";
                        }
                    }

                    System.out.println("the actual user details are : "
                            + sd);
                    stat= false;
                }

            }

        };

        saxParser.parse(file, handler);

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

}
}

2 个答案:

答案 0 :(得分:0)

ContentHandler.characters API表示 Parser将调用此方法来报告每个字符数据块。 SAX解析器可以在一个块中返回所有连续的字符数据,或者它们可以将它分成几个块

您应该在一个字符串中收集这些块

答案 1 :(得分:0)

对于节点UserDetails,解析器将多次调用

字符(char ch [],int start,int length),并且每次调用时都必须添加ch []。您可以将元素的起始名称保留为一个变量并使用qName startElement()设置它,然后检查变量以确保几次调用characters()但仍然是相同的start元素。