XmlPullParser : 提取 Tag 中的标签
XmlPullParser : extract tags in Tag
我想使用 XmlPullParser 在父标签中添加多个标签,但我不知道该怎么做。
这是我的 XML 回复:
RESPONSE:
<?xml version="1.0" encoding="UTF-8" ?>
<SecretTAG Version="2.0">
<Program name="FCVTNW">
***<ReturnValue Type="Char">TWELVE THOUSAND, THREE HUNDRED FORTY FIVE DOLLARS AND NO CENTS</ReturnValue>
<ParameterList>
<LIMIT Type="Packed">150</LIMIT>
<DECV Type="Packed">12345.00</DECV>
<ERROR Type="Char"></ERROR>***
</ParameterList>
</Program>
<End Time="2015-05-29 15:30:37.000" Elapsed="110" Clock="1"/>
<Info Built="02.00.002 Jan 6 2015 15:46:19 - i5/520"/>
</SecretTag>
这是我的解析器:
public void parse(String xml) throws XmlPullParserException, IOException {
// convert String into InputStream
// Does not work
InputStream in = new ByteArrayInputStream(xml.getBytes());
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();//Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in, null);
parser.nextTag();
readEntry(parser);
} finally {
in.close();
}
}
private void readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "Easycom");
this.Program = null;
this.ReturnValue = null;
this.ParameterList = null;
this.FATAL = null;
this.End = null;
do {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("Program")) {
this.Program = readProgram(parser);
parser.next();
} else if (name.equals("ReturnValue")) {
this.ReturnValue = readReturnValue(parser);
parser.next();
} else if (name.equals("ParameterList")) {
this.ParameterList = readParameterList(parser);
parser.nextTag();
} else if (name.equalsIgnoreCase("FATAL")) {
this.FATAL = readFATAL(parser);
parser.next();
} else if (name.equals("End")) {
this.End = readEnd(parser);
parser.next();
} else {
parser.next();
}
}while(parser.next() != XmlPullParser.END_TAG);
private String readParameterList(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "ParameterList");
String ParameterList = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "ParameterList");
return ParameterList;
}
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}
我想提取这个:
<ReturnValue Type="Char">TWELVE THOUSAND, THREE HUNDRED FORTY FIVE DOLLARS AND NO CENTS</ReturnValue>
<ParameterList>
<LIMIT Type="Packed">150</LIMIT>
<DECV Type="Packed">12345.00</DECV>
<ERROR Type="Char"></ERROR>***
</ParameterList>
如何做到这一点?
感谢您的回复! :)
我发现:我使用过 StringBuilder。
private String readParameterList(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "ParameterList");
StringBuilder builder = new StringBuilder();
String result =null;
while (!(parser.next() == XmlPullParser.END_TAG && parser.getName().equalsIgnoreCase("ParameterList")) ) {
String ParameterList = parser.getText();
result = builder.append(ParameterList).toString();//.append(" ")
}
//String ParameterList =readText(parser);
//parser.require(XmlPullParser.END_TAG, ns, "ParameterList");
result = result.replaceAll("\n", "");
return result;
}
我想使用 XmlPullParser 在父标签中添加多个标签,但我不知道该怎么做。
这是我的 XML 回复:
RESPONSE:
<?xml version="1.0" encoding="UTF-8" ?>
<SecretTAG Version="2.0">
<Program name="FCVTNW">
***<ReturnValue Type="Char">TWELVE THOUSAND, THREE HUNDRED FORTY FIVE DOLLARS AND NO CENTS</ReturnValue>
<ParameterList>
<LIMIT Type="Packed">150</LIMIT>
<DECV Type="Packed">12345.00</DECV>
<ERROR Type="Char"></ERROR>***
</ParameterList>
</Program>
<End Time="2015-05-29 15:30:37.000" Elapsed="110" Clock="1"/>
<Info Built="02.00.002 Jan 6 2015 15:46:19 - i5/520"/>
</SecretTag>
这是我的解析器:
public void parse(String xml) throws XmlPullParserException, IOException {
// convert String into InputStream
// Does not work
InputStream in = new ByteArrayInputStream(xml.getBytes());
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();//Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in, null);
parser.nextTag();
readEntry(parser);
} finally {
in.close();
}
}
private void readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "Easycom");
this.Program = null;
this.ReturnValue = null;
this.ParameterList = null;
this.FATAL = null;
this.End = null;
do {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("Program")) {
this.Program = readProgram(parser);
parser.next();
} else if (name.equals("ReturnValue")) {
this.ReturnValue = readReturnValue(parser);
parser.next();
} else if (name.equals("ParameterList")) {
this.ParameterList = readParameterList(parser);
parser.nextTag();
} else if (name.equalsIgnoreCase("FATAL")) {
this.FATAL = readFATAL(parser);
parser.next();
} else if (name.equals("End")) {
this.End = readEnd(parser);
parser.next();
} else {
parser.next();
}
}while(parser.next() != XmlPullParser.END_TAG);
private String readParameterList(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "ParameterList");
String ParameterList = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "ParameterList");
return ParameterList;
}
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}
我想提取这个:
<ReturnValue Type="Char">TWELVE THOUSAND, THREE HUNDRED FORTY FIVE DOLLARS AND NO CENTS</ReturnValue>
<ParameterList>
<LIMIT Type="Packed">150</LIMIT>
<DECV Type="Packed">12345.00</DECV>
<ERROR Type="Char"></ERROR>***
</ParameterList>
如何做到这一点?
感谢您的回复! :)
我发现:我使用过 StringBuilder。
private String readParameterList(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "ParameterList");
StringBuilder builder = new StringBuilder();
String result =null;
while (!(parser.next() == XmlPullParser.END_TAG && parser.getName().equalsIgnoreCase("ParameterList")) ) {
String ParameterList = parser.getText();
result = builder.append(ParameterList).toString();//.append(" ")
}
//String ParameterList =readText(parser);
//parser.require(XmlPullParser.END_TAG, ns, "ParameterList");
result = result.replaceAll("\n", "");
return result;
}