简体   繁体   中英

Android - Parsing Custom XML File Format into string

I have an xml file with a custom format which I am trying to store in a string. I am still a beginner with java and have search the forums, but the xml questions asked already appear to relate to xml formats in standard xml format.

The xml format I have to work with is as follows:

<playList baseUrl= "Webaddress"
<file name="xxxxxxx.xxx" showTime="YYYY-MM-DD HH:MM" />
<file name="xxxxxxx.xxx" showTime="YYYY-MM-DD HH:MM" />
.......
</playList>

I've been trying the sample code for XML parsing, but this is set for particular. I've been trying for hours to overcome this, but I'm stuck even trying to pull in the string into my code.

I have been looking at the tutorial here http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/ but I'm still stuck importing the xml file. I believe it is with the hashmap code as my xml format doesn't match the standard tags <> <>.

Here is the code I have at present:

static final String KEY_ITEM ="playlist";
static final String KEY_NAME = "name";
static final String KEY_DESC = "showTime";


    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_ITEM);


    // looping through all item nodes <item>
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        map.put(KEY_COST, getString(e))
        // adding each child node to HashMap key => value
        map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
        map.put(KEY_DESC, parser.getValue(e, KEY_DESC));

        // adding HashList to ArrayList
        menuItems.add(map);

Any help or assistance would be greatly appreciated.

Thanks,

If I understood well you want to have this follwing output :

xxxxxxx.xx1

YYYY-MM-DD HH:MM

etc....

I've done this by getting NamedNodeMap in doing this following :

To get the attributes attributes of the first line :

xxxxxxx.xx1

doc.getElementsByTagName("file").item(1).getAttributes().item(0).getTextContent();

YYYY-MM-DD HH:MM

doc.getElementsByTagName("file").item(0).getAttributes().item(1).getTextContent();

The second line :

xxxxxxx.xx2

doc.getElementsByTagName("file").item(1).getAttributes().item(0).getTextContent();

YYYY-MM-DD HH:MM

doc.getElementsByTagName("file").item(1).getAttributes().item(1).getTextContent();

If you want to have only the text between the quotes just add .getTextContent() . If not you will get the following output -> name="xxxxxxx.xx2" .
Then you can set these values to your HashMap.

You should also take a look of your xml file :

<?xml version="1.0" encoding="UTF-8"?>
<playList baseUrl= "Webaddress">
<file name="xxxxxxx.xx1" showTime="YYYY-MM-DD HH:MM" />
<file name="xxxxxxx.xx2" showTime="YYYY-MM-DD HH:MM" />
</playList>

Works for me

Hope it's help ;)

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