简体   繁体   English

Android-将自定义XML文件格式解析为字符串

[英]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. 我有一个自定义格式的xml文件,尝试将其存储在字符串中。 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. 我仍然是使用Java的初学者,并且已经搜索了论坛,但是所询问的xml问题似乎已经与标准xml格式的xml格式有关。

The xml format I have to work with is as follows: 我必须使用的xml格式如下:

<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. 我一直在尝试XML解析的示例代码,但这是为特定设置的。 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. 我一直在这里的http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/中查看该教程,但是我仍然无法导入xml文件。 I believe it is with the hashmap code as my xml format doesn't match the standard tags <> <>. 我相信它与hashmap代码一起使用,因为我的xml格式与标准标记<> <>不匹配。

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 xxxxxxx.xx1

YYYY-MM-DD HH:MM YYYY-MM-DD HH:MM

etc.... 等等....

I've done this by getting NamedNodeMap in doing this following : 我通过在以下操作中获取NamedNodeMap来完成此操作:

To get the attributes attributes of the first line : 要获得第一行的attribute属性:

xxxxxxx.xx1 xxxxxxx.xx1

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

YYYY-MM-DD HH:MM YYYY-MM-DD HH:MM

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

The second line : 第二行:

xxxxxxx.xx2 xxxxxxx.xx2

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

YYYY-MM-DD HH:MM 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() . 如果只想在引号之间添加文本,则只需添加.getTextContent()即可 If not you will get the following output -> name="xxxxxxx.xx2" . 如果没有,您将获得以下输出-> name =“ xxxxxxx.xx2”。
Then you can set these values to your HashMap. 然后,您可以将这些值设置为HashMap。

You should also take a look of your xml file : 您还应该看一下xml文件:

<?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 ;) 希望能有所帮助;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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