简体   繁体   中英

How to get particular values from xml tags in android

<dict>
<key>Finance</key>
<dict>
<key>MainContent</key>
<array><string>Transactions</string><string>Booking Sales</string><string>Export Booking</string><string>Operation Revenue</string></array></dict>
<key>AboutUs</key>
<dict>
<key>MenuItems</key>
<array><dict><key>name</key><string>Ship Owning</string><key>submenu</key><array><string>Orient Express Lines FZCO</string><string>Transworld Bulk Carriers FZCO</string></array>
</dict>
<dict>
<key>name</key>
<string>Feeder</string>
<key>submenu</key>
<array><string>Orient Express Lines FZCO</string><string>Shreyas Shipping And Logistics Ltd</string></array>
</dict>
</dict>

In above xml file how to get <string> tag values. Is there any best efficient way to get values.

You won't get very far if your xml isn't well formed. You can use a library such as JSoup to attempt to clean up the xml.

See here: http://try.jsoup.org/~qw0Vw1_TiKI71RmfxOn9HPL8u1U

Then its a case of just using the library to clean the xml, and tell it to pick out the info you want:

package doodle;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Main {

    public static void main(String[] args) {

        Document doc = Jsoup
                .parse("<dict><key>Finance</key><dict><key>MainContent</key><array><string>Transactions</string><string>Booking Sales</string><string>Export Booking</string><string>Operation Revenue</string></array></dict><key>AboutUs</key><dict><key>MenuItems</key><array><dict><key>name</key><string>Ship Owning</string><key>submenu</key><array><string>Orient Express Lines FZCO</string><string>Transworld Bulk Carriers FZCO</string></array></dict><dict><key>name</key><string>Feeder</string><key>submenu</key><array><string>Orient Express Lines FZCO</string><string>Shreyas Shipping And Logistics Ltd</string></array></dict></dict>");

        Elements elements = doc.select("string");

        for (Element element : elements) {
            System.out.println(element.text());
        }

    }

}

Transactions Booking Sales Export Booking Operation Revenue Ship Owning Orient Express Lines FZCO Transworld Bulk Carriers FZCO Feeder Orient Express Lines FZCO Shreyas Shipping And Logistics Ltd

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM