开发者

Android - Node.getNodeValue() truncating when there is '

I'm parsing some XML with XPath.

The XML code follows:

<?xml version="1.0" encoding="UTF-8"?>
<response>
  <teldir>
    <contact>
      <nameDept>D&apos;ADAMO, Piergiorgio</nameDept>
    </contact>
  </teldir> 
</response>

Using the expression /response/teldir/contact/nameDept/text() I put the result in a Java String with Node.getNodeValue().

This string is shown in a ListView using a custom but simple layout for each item.

<TextView 
    android:id="@+id/contact_name_dept" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:textSize="20sp"
    android:textColor="#FFFFFF"
/>

I have a unit test asserting the string is "D'ADAMO, Piergiorgio".

The problem is that only when the code runs in the emul开发者_如何学Goator the item in the ListView shows "D'".

It seems that Node.getNodeValue() is truncating the string when the apostrophe occurs.

Maybe the Node DOM implementation in Android has issues?


The XPath specification requires that adjacent text nodes are concatenated, so using /text() should never give you half a text node. Unfortunately there are some careless implementations around, and if they run on a DOM that has multiple adjacent text nodes (as can often happen when entities are involved) they don't go to the trouble of merging them. It's a non-conformance and you should complain about it, but you'll be lucky to get it fixed. Meanwhile, try to get out of the habit of using /text() in your XPath expressions - it's nearly always bad practice. Instead, get the string value of the containing element using string(/response/teldir/contact/nameDept). It's very unlikely that any XPath implementation will get that one wrong (I hope!).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜