将xml转换为json android [英] converting xml to json android

查看:65
本文介绍了将xml转换为json android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i am using java-json.jar to convert the incoming xml into workable json. The only problem right now that i am having is that when the xml contains <![CDATA[ <p>some html</p> ]]> it crashes with the following log:

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属性时,一切都会顺利进行.

when i remove the CDATA properties everything goes fine and well.

我要解析的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>

有人知道问题出在哪里吗?

Does anybody know what the problem is?

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

i am using the following to actually parse the xml:

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

推荐答案

我知道这已经很晚了,但是我只是想使其工作起来,所以就到这里了.请记住,这只是快速修复,而不是最佳实践.

I know this is late, but I just wanted to make it work so here you go. Keep in mind, this is just a quick fix and not best practice.

  1. 从Github-Repo下载所有文件: https://github.com/stleary/JSON-java
  2. 打开XMLTokener.java并注释掉第69、70、71行.
  3. 您完成了.请记住,如果未关闭CDATA,将不会引发任何错误,因此这不是最佳的解决方法,但至少可以使用.
  1. Download all the files from the Github-Repo: https://github.com/stleary/JSON-java
  2. Open XMLTokener.java and comment out line 69, 70, 71.
  3. You're done. Keep in mind, if the CDATA is not closed, no error will be thrown, so this isn't the best way to go, but at least it works.

这篇关于将xml转换为json android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆