如何使用 XmlPullParser 解析 XML?
How to parse XML With XmlPullParser?
我用 XmlPullParser 读取了一个 Xml 文件,但它给出了异常(未终止的实体引用(位置:TEXT @817:4 in java.io.InputStreamReader@4cb5258) Dscp .
i reader 在这张照片之前再拍一张这样的照片
一切都很好,直到这一个。
当我从 xml 中删除 Dsec 时,一切正常,我读取了所有 xml 文件。
当我用 Dsec 阅读 xml 时,我在这个镜头中有例外
之前的镜头是这样的
<Shot>
<ShotGUID>59AA1C15D3384B5691FBFB40B0FE38BD</ShotGUID>
<Title>##Step 2-Categories of Projects</Title>
<StartTime>00:00:40.00</StartTime>
<EndTime>00:00:00.00</EndTime>
<FileName>Shot0042.jpg</FileName>
<Dscp>Step 2-Categories of Projects
Aggregate project plan identifies four separate categories of projects:
Derivative projects … those that are only incrementally different from existing offerings
Platform projects … major departures from existing offerings … the next generation
Breakthrough projects … involving a newer technology … possibly a “disruptive” technology
R&D projects … “blue sky” or visionary endeavors
</Dscp>
<Footer></Footer>
</Shot>
这是我的代码:
XmlPullParserFactory parserFactory;
parserFactory = XmlPullParserFactory.newInstance();
XmlPullParser parser = parserFactory.newPullParser();
ByteArrayInputStream byteArrayInputStreamXmlData = new ByteArrayInputStream(bufferXMLData);
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(byteArrayInputStreamXmlData, "UTF-8");
List<PageModel> pages = new ArrayList<>();
int eventType = parser.getEventType();
PageModel current = null;
while (eventType != XmlPullParser.END_DOCUMENT) {
String eltName = null;
switch (eventType) {
case XmlPullParser.START_TAG:
eltName = parser.getName();
if (eltName.equals("Shot")) {
current = new PageModel();
pages.add(current);
} else if (current != null) {
switch (eltName) {
case "ShotGUID":
current.setShotGUID(parser.nextText());
break;
case "StartTime":
current.setStartTime(parser.nextText());
break;
case "EndTime":
current.setEndTime(parser.nextText());
break;
case "FileName":
current.setFileName(parser.nextText());
break;
case "Dscp":
current.setDscp(parser.nextText());
case "Footer" :
current.setFooter(parser.nextText());
break;
}
}
break;
}
eventType = parser.next();
}
经过大量研究,我意识到我无法在 Dscp 值中使用 & 字符,我需要使用替代代码。
XML中的特殊字符是什么?
< (<), & (&), > (>), " ("), and ' (').
我用 XmlPullParser 读取了一个 Xml 文件,但它给出了异常(未终止的实体引用(位置:TEXT @817:4 in java.io.InputStreamReader@4cb5258) Dscp .
i reader 在这张照片之前再拍一张这样的照片 一切都很好,直到这一个。 当我从 xml 中删除 Dsec 时,一切正常,我读取了所有 xml 文件。 当我用 Dsec 阅读 xml 时,我在这个镜头中有例外
之前的镜头是这样的
<Shot>
<ShotGUID>59AA1C15D3384B5691FBFB40B0FE38BD</ShotGUID>
<Title>##Step 2-Categories of Projects</Title>
<StartTime>00:00:40.00</StartTime>
<EndTime>00:00:00.00</EndTime>
<FileName>Shot0042.jpg</FileName>
<Dscp>Step 2-Categories of Projects
Aggregate project plan identifies four separate categories of projects:
Derivative projects … those that are only incrementally different from existing offerings
Platform projects … major departures from existing offerings … the next generation
Breakthrough projects … involving a newer technology … possibly a “disruptive” technology
R&D projects … “blue sky” or visionary endeavors
</Dscp>
<Footer></Footer>
</Shot>
这是我的代码:
XmlPullParserFactory parserFactory;
parserFactory = XmlPullParserFactory.newInstance();
XmlPullParser parser = parserFactory.newPullParser();
ByteArrayInputStream byteArrayInputStreamXmlData = new ByteArrayInputStream(bufferXMLData);
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(byteArrayInputStreamXmlData, "UTF-8");
List<PageModel> pages = new ArrayList<>();
int eventType = parser.getEventType();
PageModel current = null;
while (eventType != XmlPullParser.END_DOCUMENT) {
String eltName = null;
switch (eventType) {
case XmlPullParser.START_TAG:
eltName = parser.getName();
if (eltName.equals("Shot")) {
current = new PageModel();
pages.add(current);
} else if (current != null) {
switch (eltName) {
case "ShotGUID":
current.setShotGUID(parser.nextText());
break;
case "StartTime":
current.setStartTime(parser.nextText());
break;
case "EndTime":
current.setEndTime(parser.nextText());
break;
case "FileName":
current.setFileName(parser.nextText());
break;
case "Dscp":
current.setDscp(parser.nextText());
case "Footer" :
current.setFooter(parser.nextText());
break;
}
}
break;
}
eventType = parser.next();
}
经过大量研究,我意识到我无法在 Dscp 值中使用 & 字符,我需要使用替代代码。
XML中的特殊字符是什么?
< (<), & (&), > (>), " ("), and ' (').