Android Xml解析“嵌入值" [英] Android Xml Parsing "embedded values"
问题描述
我很难弄清楚如何解析这个数据块:
Im having a hard time trying to figure out how to parse this block of data:
<prov version="1.1">
<characteristic type="section1">
<parm name="version" value="74"/>
<parm name="validity" value="172800"/>
</characteristic>
...
<characteristic type="section99">
<parm name="random_setting1" value="blahblah1"/>
<parm name="random_setting2" value="blahblah2"/>
<characteristic type="section99_subsection2">
<parm name="random_setting3" value="blahblah1"/>
<parm name="random_setting4" value="blahblah2"/>
</characteristic>
</characteristic>
</prov>
我在一个类似于上述示例的 xml 文件中可能有 200 多行.我想把它分成 4 个字段放入数据库:
I have probably 200+ lines in an xml file resembling the above example. Id like to break it up into 4 fields to put in a database:
一级特性
二级特征(可以为空)
设置名称
价值
但我终其一生都无法弄清楚如何做到这一点.
But I cannot for the life of me figure out how to do this.
我有这个文档生成器:
Log.d("RNM", "Starting xmlToDb");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document xmlParse = dBuilder.parse(new InputSource(new StringReader(xmlString)));
xmlParse.getDocumentElement().normalize();
NodeList nList = xmlParse.getElementsByTagName("characteristic");
for ( int tmp = 0; tmp < nList.getLength(); tmp++) {
Node nNode = nList.item(tmp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if ( nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
String charType = eElement.getAttribute("type"); //Tells me the value of characteristic
}
}
上面的工作是拉出所有的特征块并可以得到值.但我不知道如何提取位于每一个下方的 parmName 和 parmValud.
The above works to pull out all the characteristic blocks and can get the values. but I can't figure out how to extract the parmName and parmValud that lie beneath each one.
有什么例子可以解决这个问题吗?我在这里看了:http://theopentutorials.com/tutorials/android/xml/android-simple-xml-sax-parser-tutorial/ 但我不知道如何使用 saxparser 获取这些值.
Any examples out there on dealing with this? I looked here: http://theopentutorials.com/tutorials/android/xml/android-simple-xml-sax-parser-tutorial/ But I could not figure out how to grab those values with the saxparser.
推荐答案
我使用 Android 的 XmlPullParser.
I use Android's XmlPullParser.
有两个关键方法:next() 和 nextToken().next() 提供对高级解析事件的访问,而 nextToken() 允许访问较低级别的令牌.
There are two key methods: next() and nextToken(). While next() provides access to high level parsing events, nextToken() allows access to lower level tokens.
示例用法:
import java.io.IOException;
import java.io.StringReader;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
public class SimpleXmlPullApp
{
public static void main (String args[])
throws XmlPullParserException, IOException
{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput( new StringReader ( "<foo>Hello World!</foo>" ) );
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_DOCUMENT) {
System.out.println("Start document");
} else if(eventType == XmlPullParser.START_TAG) {
System.out.println("Start tag "+xpp.getName());
} else if(eventType == XmlPullParser.END_TAG) {
System.out.println("End tag "+xpp.getName());
} else if(eventType == XmlPullParser.TEXT) {
System.out.println("Text "+xpp.getText());
}
eventType = xpp.next();
}
System.out.println("End document");
}
}
这篇关于Android Xml解析“嵌入值"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!