0

次のテキストを含む xml タグを持つ sax パーサーがあります: "A & amp; B" (スペースがありません - 追加されたので & ここでは変換されません)

アンパサンドで2回変換されてエスケープされて結果が「A」になったようなものです。プロセスは次のとおりです。

Xmlファイルがダウンロードされます

InputStream _inputStream = _urlConnection.getInputStream();
                        BufferedInputStream _bufferedInputStream = new BufferedInputStream(_inputStream);
                        ByteArrayBuffer _byteArrayBuffer = new ByteArrayBuffer(64);

                        int current = 0;
                        while((current = _bufferedInputStream.read()) != -1)
                        {
                            _byteArrayBuffer.append((byte)current);
                        }

                        FileOutputStream _fileOutputStream = openFileOutput(_file, MODE_PRIVATE);

                        _fileOutputStream.write(_byteArrayBuffer.toByteArray());
                        _fileOutputStream.close();

データは endElement で Sax で変換されます

else if (inLocalName.equalsIgnoreCase(_nodeTitle))
        {
            _titleValue = currentValue;
            currentValue = "";
        }

デバッグでは、アンパサンドは既に変換されており、ハンドラーの文字メソッドで読み取るとデータが切り捨てられます。

これについて多くの質問を見てきましたが、解決策はありません。何か案は?

ありがとう

パーサー:

List<PropertiesList> _theList = null;

        try 
        {
            // Create Factory, Parser, Reader, Handler
            SAXParserFactory _saxParserFactory = SAXParserFactory.newInstance();
            SAXParser _saxParser = _saxParserFactory.newSAXParser();
            XMLReader _xmlReader = _saxParser.getXMLReader();
            HandlerReps _handler = new HandlerReps(inRegion, inAbbreviation);

            _xmlReader.setContentHandler(_handler);
            _xmlReader.parse(new InputSource(inStream));

            _theList = _handler.getTheList();
        } 

ハンドラ:

// Called when Tag Begins
    @Override
    public void startElement(String uri, String inLocalName, String inQName, Attributes inAttributes) throws SAXException 
    {
        currentElement = false;
    }

    // Called when Tag Ends
    @Override
    public void endElement(String inUri, String inLocalName, String inQName) throws SAXException 
    {
        currentElement = false;

        // Title
        if (inLocalName.equalsIgnoreCase(_nodeValue))
        {
            if (_stateValue.equalsIgnoreCase(_abbreviation) && 
                _countryValue.equalsIgnoreCase(_region))
            {
                // Construct the object
                PropertiesRegion _regionObject = new PropertiesRegion(_titleValue, _address1Value);

                cList.add(_regionObject);

                Log.d(TAG, _regionObject.toString());
            }

            _titleValue = "";
            _address1Value = "";
        }

        // Title
        else if (inLocalName.equalsIgnoreCase(_nodeTitle))
        {
            _titleValue = currentValue;
            currentValue = "";
        }

        // Address1
        else if (inLocalName.equalsIgnoreCase(_nodeAddress1))
        {
            _address1Value = currentValue;
            currentValue = "";
        }
    }

    // Called to get Tag Characters
    @Override
    public void characters(char[] inChar, int inStart, int inLength) throws SAXException 
    {
        if (currentElement) 
        {
            currentValue = new String(inChar, inStart, inLength);
            currentElement = false;
        }
    }
4

1 に答える 1

1

これが問題の原因である可能性が非常に高いです:

    if (currentElement) 
    {
        currentValue = new String(inChar, inStart, inLength);
        currentElement = false;
    }

テキスト コンテンツ ノードごとに、SAX パーサーは複数の characters() イベントをハンドラーに送信する場合があります。これらすべてのイベントを連結した場合にのみ、テキスト全体を取得できます。ただし、コードでは、これらのイベントの最初のイベントのみが使用されますcurrentElement = false

問題はアンパサンド変換ではありません。原則として、問題を説明するときは、想定される原因ではなく、症状のみを説明する方がよい場合がよくあります。

于 2012-04-14T19:36:42.677 に答える