2012-04-14 67 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