gpt4 book ai didi

java - 解析 string.xml 文件时出现 XmlPullParserException

转载 作者:行者123 更新时间:2023-12-01 12:00:33 26 4
gpt4 key购买 nike

我正在尝试解析一个类似于 res 文件夹下的 string.xml 文件的 xml 文件。但在解析时我遇到了异常。 下面是我的 xml 文件。

<resources>

<!-- Enrollment Messages -->
<string name="configure_email_text">No corporate email accounts have been configured on this device. To configure them, click <b>Configure Email</b></string>
<string name="invalid_credentials">Authentication failed. Enter valid credentials.</string>
<string name="invalid_passcode">Authentication failed. Enter valid Passcode.</string>
<string-array name="lang_support">
<item>en_US</item>
<item>en_GB</item>
</string-array>
<string name="picker_combined_view_fmt">Combined view (<xliff:g id="count">%s</xliff:g>)</string>
</resources>

下面是我用来解析 XML 的代码。

 private void loadXML()
{
// TODO Auto-generated method stub
InputStream ins = null;
try{
//Debug.startMethodTracing("parsingdetail");

ins=getAssets().open("strings.xml");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
// factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
xpp.setInput(ins, null);
parseXml(xpp);
int j=hMap.size();
if(j>0){
System.out.println("Anuj Kumar Jha "+j);
}
}catch(XmlPullParserException e){
System.out.println("anuj kumar jha"+e);
e.printStackTrace();
}catch(IOException e){
System.out.println("anuj kumar jha"+e);
e.printStackTrace();
}
finally{
if(ins!=null){
try {
ins.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}


private void parseXml(XmlPullParser parser) throws XmlPullParserException,IOException
{
// TODO Auto-generated method stub
int eventType=parser.getEventType();
while(eventType!=XmlPullParser.END_TAG){
String key=null;
String value=null;
switch(eventType){
case XmlPullParser.START_TAG:
String name=parser.getName();
if(name.equalsIgnoreCase("string")){
key=parser.getAttributeValue(null,"name");
value=parser.nextText();
hMap.put(key, value);
if(parser.getEventType()!=XmlPullParser.END_TAG){
parser.nextTag();
}
}
}
eventType=parser.next();
}
}

下面是我收到的异常

01-18 12:19:10.506: I/System.out(16115): anuj kumar jhaorg.xmlpull.v1.XmlPullParserException: END_TAG expected (position:START_TAG <b>@5:137 in java.io.InputStreamReader@42b54168) 

我认为这是因为第一个字符串标签中的标签。如何解决这个问题?

最佳答案

遵循这个模式(我在 http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html 上找到的):

     int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_TAG) {
String name=parser.getName();
if(name.equalsIgnoreCase("string")){
key=parser.getAttributeValue(null,"name");
String value = collectText(parser);
hMap.put(key, value);
}
}
eventType = xpp.next();
}

private String collectText(XmlPullParser parser){
StringBuilder sb = new StringBuilder();
int eventType=parser.getEventType();
while (eventType != XmlPullParser.END_TAG) {
if(eventType == XmlPullParser.START_TAG){
sb.append( collectText(parser) );
} else if(eventType == XmlPullParser.TEXT) {
sb.append( xpp.getText() );
}
eventType = xpp.next();
}
return sb.toString();
}

关于java - 解析 string.xml 文件时出现 XmlPullParserException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28007857/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com