我有一个自定义格式的xml文件,我试图将其存储在字符串中。我仍然是java的初学者并且已经搜索了论坛,但是xml问题已经显示出与标准xml格式的xml格式有关。
我必须使用的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>
我一直在尝试用于XML解析的示例代码,但这是特别设置的。我已经花了好几个小时来克服这个问题,但我甚至试图将字符串插入我的代码中。
我一直在看这里的教程http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/,但我仍然无法导入xml文件。我相信它是使用hashmap代码,因为我的xml格式与标准标签不匹配&lt;&gt; &LT;&GT;
以下是我目前的代码:
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);
非常感谢任何帮助或帮助。
谢谢,
答案 0 :(得分:0)
如果我理解得很好,你想要有这样的结果:
xxxxxxx.xx1
YYYY-MM-DD HH:MM
等...
我通过让NamedNodeMap执行以下操作来完成此操作:
获取第一行的属性属性:
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();
第二行:
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();
如果您只想在引号之间添加文字,只需添加 .getTextContent()。
如果没有,您将获得以下输出 - &gt; name =“xxxxxxx.xx2”。
然后,您可以将这些值设置为HashMap。
您还应该看看您的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>
为我工作
希望它有所帮助;)