【问题标题】:xml to json in androidandroid中的xml到json
【发布时间】:2012-06-06 05:35:01
【问题描述】:

我想要做的是我正在制作一个应用程序引擎,我必须在其中解析类似于下面示例的 xml 和其他此类 xml。我想把它转换成json ....有什么办法吗?

 - <application>
    - <!--  [Start] Application Section 
      --> `enter code here`
      <app_type>app_business</app_type> 
    - <app_info>
      <app_name>App test for test user</app_name> 
      <app_description>app description</app_description> 
      <app_keywords>4464</app_keywords> 
      <app_website>www.google.co.in</app_website> 
      <app_logo>1335504801.jpg</app_logo> 
      <splash_screen_logo>Splash.jpg</splash_screen_logo> 
      <mobile_platform>0</mobile_platform> 
      <header_bg /> 
      <bg_image /> 
      <bg_color /> 
      <usr_name>Test Singh</usr_name> 
      <usr_username>test</usr_username> 
      <usr_email>test@gmail.com</usr_email> 
      <usr_registerDate>2012-04-26 10:21:00</usr_registerDate> 
      <usr_plan_id>1</usr_plan_id> 
      <usr_lastvisitDate>2012-04-30 04:24:34</usr_lastvisitDate> 
      <usr_plan_id>1</usr_plan_id> 
      <usr_gender>Male</usr_gender> 
      <usr_contactno>12321432</usr_contactno> 
      <usr_address>Jaipur</usr_address> 
      <usr_profile_picture>1234514Sunset.jpg</usr_profile_picture> 
      <usr_dob>2012-04-09</usr_dob> 
      </app_info>
    - <app_home>
    - <!--  [Start]Home Section 
      --> 
    - <!--  Home properties 
      --> 
    - <home_properties>
      <tab_icon>home.png</tab_icon> 
      <tab_title>Home</tab_title> 
      <banner_text>Home Banner Text here.</banner_text> 
      <bg_image>business_logo1.png</bg_image> 
      </home_properties>
    - <!--  Home Buttons 
      --> 
    - <home_buttons>
    - <button>
      <button_type>app_portfolio</button_type> 
      <button_text>Portfolio</button_text> 
      <button_image>button.jpg</button_image> 
      </button>
    - <button>
      <button_type>app_websiteUrl</button_type> 
      <button_text>Web Url</button_text> 
      <button_image>button.jpg</button_image> 
      </button>
    - <button>
      <button_type>app_contact</button_type> 
      <button_text>Contact</button_text> 
      <button_image>button.jpg</button_image> 
      </button>
      </home_buttons>
      </app_home>
    - <!--  [End]Home Section 
      --> 
    - <app_aboutus>
    - <!--  [Start]About Us Section 
      --> 
    - <!--  [Start]Home properties 
      --> 
    - <aboutus_properties>
      <tab_icon>aboutus.png</tab_icon> 
      <tab_title>About Us Tab</tab_title> 
      <banner_text>About Us Banner Text</banner_text> 
      <bg_image>bg-image.jpg</bg_image> 
      <image>about-img.jpg</image> 
      <description>About us description</description> 
      </aboutus_properties>
    - <!--  [End]Home properties 
      --> 
      </app_aboutus>
    - <!--  [End]About Us Section 
      --> 
    - <app_services>
    - <!--  [Start]Services Section 
      --> 
    - <!-- [Start] Services properties 
      --> 
    - <services_properties>
      <tab_icon>2.png</tab_icon> 
      <tab_title>Services Tab Title</tab_title> 
      <banner_text>Services Banner Text</banner_text> 
      <bg_image>bg-image.jpg</bg_image> 
      <image>services.jpg</image> 
      <description>Services Description</description> 
      </services_properties>
    - <!--  [End]Services properties 
      --> 
      </app_services>
    - <!--  [End]Services Section 
      --> 
    - <app_more>
    - <!--  [Start]More Section 
      --> 
    - <!--  More properties 
      --> 
    - <more_properties>
      <tab_icon>more.png</tab_icon> 
      <tab_title>More</tab_title> 
      <banner_text>More Banner text</banner_text> 
      <bg_image>bg-image.jpg</bg_image> 
      </more_properties>
    - <!--  More Buttons 
      --> 
    - <more_buttons>
    - <button>
      <button_type>member profile</button_type> 
      <button_text>Member Profile</button_text> 
      <button_image>button.jpg</button_image> 
      </button>
    - <button>
      <button_type>map</button_type> 
      <button_text>More Button map</button_text> 
      <button_image>button.jpg</button_image> 
      </button>
    - <button>
      <button_type>audiovideo</button_type> 
      <button_text>Audio Video Streaming</button_text> 
      <button_image>button.jpg</button_image> 
      </button>
    - <button>
      <button_type>news</button_type> 
      <button_text>News</button_text> 
      <button_image>button.jpg</button_image> 
      </button>
    - <button>
      <button_type>services</button_type> 
      <button_text>Services</button_text> 
      <button_image>button.jpg</button_image> 
      </button>
      </more_buttons>
      </app_more>
    - <!--  [End]More Section 
      --> 
    - <app_news>
    - <!--  [Start]News Section 
      --> 
    - <!--  News properties 
      --> 
    - <news_properties>
      <tab_icon>2.png</tab_icon> 
      <tab_title>News Tab Title</tab_title> 
      <banner_text>News Banner Text</banner_text> 
      <bg_image>bg-image.jpg</bg_image> 
      <rss_url>http://newsrss.bbc.co.uk/rss/sportonline_world_edition/front_page/rss.xml</rss_url> 
      </news_properties>
      </app_news>
    </application>

【问题讨论】:

    标签: android xml parsing


    【解决方案1】:

    通过使用this lib,您可以进行XML 到JSON 的转换

    String xmlString = "<Root><FName>Hello</FName><LName>World</LName></Root>";
    XMLSerializer serializer = new XMLSerializer();  
    JSON json = serializer.read( xmlString );
    

    查看 this 链接以供参考。

    【讨论】:

    • 有没有办法通过循环来转换整个xml,因为我有很多这样的节点......那个xml不完整......
    【解决方案2】:

    首先,请注意您的 XML 格式错误,包含大量额外的“-”字符。您应该在尝试将 XML 转换为 JSON 之前修复它们。

    我刚刚为 Android Studio 发布了一个“XML to JSON”库。 易于与 gradle 集成,使用简单,可配置。如果你愿意,可以试一试。

    XmlToJson 在 GitHub 上可用: https://github.com/smart-fun/XmlToJson

    【讨论】:

    • 我正在尝试使用您的 XmlToJson 库转换大小为 28 MB(约 81k 行)的 XMLTV xml 文件,但应用程序正在崩溃。你有什么建议吗?
    • 你好,很抱歉它崩溃了。你能在我的 github 上的 Issues 中添加一张票吗?调查会更容易。谢谢。第一个猜测是内存不足,但可能是其他原因。
    【解决方案3】:

    Underscore-java 有静态方法U.xmlToJson(xml)。我是项目的维护者。

    【讨论】:

      猜你喜欢
      • 2019-04-14
      • 1970-01-01
      • 1970-01-01
      • 2017-06-11
      • 1970-01-01
      • 2010-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多