4

我想做的是我正在制作一个应用程序引擎,我必须在其中解析类似于下面的示例和其他此类 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>
4

3 回答 3

5

通过使用这个库,您可以进行XML 到 JSON 的转换

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

查看链接以供参考。

于 2012-06-06T06:01:15.153 回答
0

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

于 2019-08-28T02:56:15.397 回答
0

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

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

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

于 2016-11-10T22:07:29.400 回答