将xml转换为json android

时间:2015-03-26 15:59:23

标签: android xml json

我正在使用java-json.jar将传入的xml转换为可用的json。我现在唯一的问题是,当xml包含<![CDATA[ <p>some html</p> ]]>时,它会崩溃并显示以下日志:

03-26 16:54:56.176  30119-30119/nl.avrotros.opgelicht E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: nl.avrotros.opgelicht, PID: 30119
    java.lang.NoSuchMethodError: No virtual method end()Z in class Lorg/json/XMLTokener; or its super classes (declaration of 'org.json.XMLTokener' appears in /data/app/nl.avrotros.opgelicht-1/base.apk)
            at org.json.XMLTokener.nextCDATA(XMLTokener.java:69)
            at org.json.XML.parse(XML.java:165)
            at org.json.XML.parse(XML.java:272)
            at org.json.XML.parse(XML.java:272)
            at org.json.XML.parse(XML.java:272)
            at org.json.XML.parse(XML.java:272)
            at org.json.XML.parse(XML.java:272)
            at org.json.XML.toJSONObject(XML.java:369)
            at nl.avrotros.opgelicht.api.Opgelicht.testxmltojson(Opgelicht.java:84)
            at nl.avrotros.opgelicht.api.Opgelicht.<init>(Opgelicht.java:78)
            at nl.avrotros.opgelicht.api.Opgelicht.getInstance(Opgelicht.java:395)
            at nl.avrotros.opgelicht.activity.MainActivity.onCreate(MainActivity.java:38)
            at android.app.Activity.performCreate(Activity.java:5953)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
            at android.app.ActivityThread.access$800(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

当我删除CDATA属性时,一切顺利。

我想解析的xml:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
  <hits>17</hits>
  <max-score>14.431101</max-score>
  <rows>1</rows>
  <start>0</start>
  <facets>
    <category>
      <facet value="malware" count="4" />
      <facet value="offline webshops" count="4" />
      <facet value="opgeletopinternet.nl" count="1" />
      <facet value="overig" count="1" />
      <facet value="spookfacturen" count="1" />
      <facet value="valse e-mails" count="5" />
      <facet value="webshops" count="2" />
    </category>
    <facet01 />
  </facets>
  <item id="1" score="14.431101">
    <created>1</created>
    <properties>
      <property>
        <key>title</key>
        <value>'Pas op voor studionstore.com!'</value>
      </property>
      <property>
        <key>teasertitle</key>
        <value>'Pas op voor studionstore.com!'</value>
      </property>
      <property>
        <key>teasertext</key>
        <value>Opgeletopinternet.nl waarschuwt voor de malafide webwinkel studionstore·com.</value>
      </property>
      <property>
        <key>intro</key>
        <value>Opgeletopinternet.nl waarschuwt voor de malafide webwinkel studionstore·com.</value>
      </property>
      <property>
        <key>onlinedate</key>
        <value>2015-03-26T12:56:54.000000Z</value>
      </property>
      <property>
        <key>episodeonair</key>
        <value>2015-03-26T12:51:00.000000Z</value>
      </property>
      <property>
        <key>text</key>
        <value><![CDATA[<p>De malafide webwinkel misbruikt de gegevens van een bonafide bedrijf. Bovendien adverteren ze met te goedkope prijzen. ook hier geldt: wat te mooi lijkt om waar te zijn, is vaak niet waar.<br /><br />Zie hieronder de site.<br /><br /></p>]]></value>
      </property>
      <property>
        <key>status</key>
        <value>online</value>
      </property>
      <property>
        <key>category</key>
        <value>Webshops</value>
      </property>
      <property>
        <key>programtitle</key>
        <value>1</value>
      </property>
      <property>
        <key>programid</key>
        <value>1</value>
      </property>
      <property>
        <key>owner</key>
        <value>1</value>
      </property>
      <property>
        <key>source</key>
        <value>typo3</value>
      </property>
      <property>
        <key>collection</key>
        <value>alerts</value>
      </property>
      <property>
        <key>portal</key>
        <value>1</value>
      </property>
      <property>
        <key>type</key>
        <value>article</value>
      </property>
      <property>
        <key>contenttype</key>
        <value>article</value>
      </property>
      <property>
        <key>origintype</key>
        <value>xml</value>
      </property>
      <property>
        <key>resized</key>
        <value>true</value>
      </property>
      <property>
        <key>converted</key>
        <value>true</value>
      </property>
      <property>
        <key>image</key>
        <value>
          <image default="true">
            <imageUrl>http://staticextern.avrotros.nl/images/cc4/OP_tx_news_studionstore.jpg</imageUrl>
          </image>
        </value>
      </property>
    </properties>
    <updated>2015-03-26T13:09:28.650Z</updated>
  </item>
</resources>

有人知道问题是什么吗?

我正在使用以下内容来实际解析xml:

return XML.toJSONObject(xmlString).toString();

1 个答案:

答案 0 :(得分:0)

我知道这已经很晚了,但我只是想让它发挥作用,所以你走了。请记住,这只是一个快速修复,而不是最佳实践。

  1. 从Github-Repo下载所有文件: https://github.com/stleary/JSON-java
  2. 打开XMLTokener.java并注释掉第69,70,71行。
  3. 你完成了。请注意,如果CDATA未关闭,则不会引发任何错误,因此这不是最佳方式,但至少可行。