2012-04-14 21 views
0

我有一個包含以下文本的XML標記SAX解析器:「A &藍調」(有沒有空間,在那裏 - 添加,這樣就不會轉化爲&這裏)Android的 - XML符號轉換

就好像它被轉換了兩次,並且由於「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

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; 
     } 
    } 
+0

請向我們展示更多實際解析XML的代碼。從URL下載文件到文件是無聊的。 :-)除非你在openFileOutput()中做錯了什麼。此外,下載在這裏以非常低效的方式完成,但這是一個不同的問題。簡而言之:不要使用InputStream/OutputStream的單字節方法。 – 2012-04-14 17:43:17

+0

我添加了下載代碼以防萬一有什麼相關的東西我不知道 - 因爲在&正在轉換。我用DOM解析器完成了完全相同的過程,但性能不可接受。輸出是正確的,但。 – user1222760 2012-04-14 17:55:31

+0

仍然沒有足夠的上下文,但我仍然嘗試了一下。請參閱下面的答案。 – 2012-04-14 19:37:06

回答

1

這很可能是你的問題的原因:

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

對於每個文本內容節點, SAX解析器可能會發送d多個字符()事件到您的處理程序。如果連接所有這些事件,則只能獲得整個文本。但在你的代碼中,只有這些事件中的第一個被使用,因爲你設置了currentElement = false

問題不是&字符轉換。作爲一般規則,當你描述一個問題時,最好只描述症狀,而不是任何假設的原因。

+0

好的 - 我明白了。這不是轉換。我認爲'&'與裸號&影響Sax處理輸出的方式。它沒有。 – user1222760 2012-04-14 22:24:04

+0

該文本(非常有可能)分成多個事件_因爲'&'。 SAX是一個非常注重性能的API,這就是爲什麼它將文本作爲多個事件發送。解析器讀取字符數組,如果它發現一個'&',它會將所有內容發送到'&'處理程序,然後發送一個未轉義的'&'字符,然後發送'&'後面的數組其餘部分。通過這種方式,解析器可以避免創建新的字符數組,以便隱藏「&」。 (請注意,這是SAX解析器可能工作的一種方式,還有其他可能性。) – 2012-04-14 22:24:35

+0

我現在關注你。我讀過'&',而一個裸的&符號影響Sax處理/構建字符數組的方式。顯然這不是事實。無論如何,做出了您所建議的更改,現在它完美地運行。我感謝幫助! – user1222760 2012-04-14 22:42:20