我想解析我从数据库软件导出的XML文件,用于我的Android应用程序。 但是有些标签有这样的参数:
<row>
<value column="Index" null="false">1</value>
<value column="Front" null="false">INFO</value>
<value column="Back" null="false">INFO</value>
<value column="Check" null="false">0</value>
</row>
在尝试查找开始标记以解析它时,我指定了什么字符串值? (例如:找到我将开始抽头与“行”进行比较的行,如果它返回真,我计算数据。我该怎样对每个值做什么,分别是Index,Front,Back和Check?)
我的java代码如下
try{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
InputStream stream = context.getResources().openRawResource(com.Whydea.chemistryhelper.R.raw.appxml);
xpp.setInput(stream, null);
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT){
if(eventType==XmlPullParser.START_TAG){
handleStartTag(xpp.getName()); //handels Start Tag
} else if (eventType==XmlPullParser.END_TAG){
handleEndTag(xpp.getName());
Ctag=null;
} else if (eventType==XmlPullParser.TEXT){
handleText(xpp.getText());
}
eventType=xpp.next();
}
}catch (NotFoundException e){
Log.d("XMLpp",e.getMessage());
}catch (XmlPullParserException e){
Log.d("XMLpp",e.getMessage());
}catch (IOException e){
Log.d("XMLpp",e.getMessage());
}
`
编辑:
每个“value”开始标记都有自己的属性(column = ...),我该如何访问它们?
例如:要访问一行,我有一个值为“row”的String常量,并检查start标记是否与之对应,并且它有效。但是当我声明一个值为“value column = \”的字符串常量时,检查\“null = \”false \“”(我必须使用\其他明智的“给出错误”),它找不到找到那个开始标记。那么什么我的常数应该是吗?
答案 0 :(得分:0)
如果我正确理解你的问题然后获得你需要做的每个值,基本上你想获得xml标签内每个属性的值
int attributeCount = xpp.getAttributeCount();
for(int i = 0; i<attributeCount; i++){
String name = xpp.getAttributeName(i);
//Log.d(TAG, "Name: "+name);
if(name != null && name.equalsIgnoreCase("column")){
return Integer.parseInt(xpp.getAttributeValue(i));
}
}
因此,一旦遇到该行,您就会在找到之后查找开始标记“值”,然后使用上面的代码获取属性的各个值。
根据您的评论,如果您想获取XML标记的文本值,则必须使用getText()方法。找到START_TAG值后,执行以下代码:
eventType = xpp.next();
if(eventType == XmlPullParser.TEXT){
String text = xpp.getText();
}
对于xml标记'INFO'值,它将返回'INFO'
答案 1 :(得分:0)
您是否开发了生成XML文件的应用程序?如果是这样,你为什么不改变它?如果XML具有以下格式,则解析XML要容易得多:
<item Index="1" Front="INFO" Back="INFO" Check="0"/>
<item Index="2" Front="INFO" Back="INFO" Check="1"/>
答案 2 :(得分:0)
try {
final Service S = new Service();
String xmlString = S.ImportAllPollBoothStatus(IMEI,asscd, boothno);
if(xmlString.toLowerCase().trim().equals("false")){
return false;
}
DocumentBuilderFactory docFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlString));
Document doc = docBuilder.parse(is);
NodeList nodes = doc.getElementsByTagName("HT");
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
NodeList blockidnodes = doc.getElementsByTagName("Table");
for (int blockidcount = 0; blockidcount < blockidnodes
.getLength(); blockidcount++) {
NodeList PollpercentId = element
.getElementsByTagName("PollpercentId");
Element line1 = (Element) PollpercentId.item(blockidcount);
NodeList asscd1 = element
.getElementsByTagName("asscd");
Element line2 = (Element) asscd1.item(blockidcount);
NodeList pollgcd = element
.getElementsByTagName("pollgcd");
Element line3 = (Element) pollgcd.item(blockidcount);
NodeList SessionYearIdref = element
.getElementsByTagName("SessionYearIdref");
Element line4 = (Element) SessionYearIdref.item(blockidcount);
NodeList MaleVoters = element
.getElementsByTagName("MaleVoters");
Element line5 = (Element) MaleVoters.item(blockidcount);
NodeList FemaleVoters = element
.getElementsByTagName("FemaleVoters");
Element line6 = (Element) FemaleVoters.item(blockidcount);
NodeList UpdatedDate = element
.getElementsByTagName("UpdatedDate");
Element line7 = (Element) UpdatedDate.item(blockidcount);
NodeList timeslot = element
.getElementsByTagName("timeslot");
Element line8 = (Element) timeslot.item(blockidcount);
this.db.insertVotingStatus(
getCharacterDataFromElement(line1),
getCharacterDataFromElement(line2),
getCharacterDataFromElement(line3),
getCharacterDataFromElement(line4),
getCharacterDataFromElement(line5),
getCharacterDataFromElement(line6),
getCharacterDataFromElement(line7),
getCharacterDataFromElement(line8));
}
}
}
catch (Exception e) {
Log.e("EXCEPTION DURING VIDHANSABHA INSERION",
"======Insert-VIDHANSABHA-DETAILS=====================" + e);
return false;
}
return true;