2012-06-06 158 views
4

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

- <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>[email protected]</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> 

回答

5

使用this lib中,你可以做XML到JSON的转换

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

来看一下,this链接以供参考。

+0

有没有办法通过循环,因为我有很多这样的节点对整个XML转换....这XML并没有完成.... – Rahulkapil

0

首先,请注意您的XML与许多额外的“ - ”字符有误。在尝试将XML转换为JSON之前,您应该修复它们。

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

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