我的资源中有XML文件,看起来像这样(删除了其他阶段元素):
<?xml version="1.0" encoding="utf-8"?>
<schedule name="Irgend ein besondere Zuchtname">
<stages>
<stage interval="0" token="3">
<name>Name</name>
<descrition>Some description...</descrition>
<image>image_to_retrieve.jpg</image>
</stage>
</stages>
</schedule>
我想获取特定阶段(通过令牌)的图像元素的文本。我的方法是:
public String GetImage(int stageToken) {
// because this is a helper class, the context is set in the constructor
XmlResourceParser parser = context.getResources().getXml(R.xml.schedule_default);
XPath xPath = XPathFactory.newInstance().newXPath();
String image;
try {
// error occurs here:
image = xPath.evaluate("//stage[@token=3]/image/text()", parser);
} catch (XPathExpressionException e) {
Log.e(LOG_TAG, "Error evaluating xpath expression.");
e.printStackTrace();
return null;
}
parser.close();
return image;
}
表达式不应该是问题,因为我用其他XPath程序测试了它。与XML的有效性相同。 这是我得到的例外:
01-17 00:12:05.044 30417-30417/at.beekeeper W/System.err: javax.xml.xpath.XPathExpressionException: javax.xml.transform.TransformerException: Attempt to invoke interface method 'org.apache.xml.dtm.DTMAxisTraverser org.apache.xml.dtm.DTM.getAxisTraverser(int)' on a null object reference
01-17 00:12:05.045 30417-30417/at.beekeeper W/System.err: at org.apache.xpath.jaxp.XPathImpl.evaluate(XPathImpl.java:295)
01-17 00:12:05.045 30417-30417/at.beekeeper W/System.err: at org.apache.xpath.jaxp.XPathImpl.evaluate(XPathImpl.java:371)
01-17 00:12:05.045 30417-30417/at.beekeeper W/System.err: at at.beekeeper.util.ScheduleParser.GetImage(ScheduleParser.java:108)
01-17 00:12:05.045 30417-30417/at.beekeeper W/System.err: at at.beekeeper.view.StageActivity.onCreate(StageActivity.java:52)
01-17 00:12:05.045 30417-30417/at.beekeeper W/System.err: at android.app.Activity.performCreate(Activity.java:5953)
01-17 00:12:05.045 30417-30417/at.beekeeper W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
01-17 00:12:05.046 30417-30417/at.beekeeper W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
01-17 00:12:05.046 30417-30417/at.beekeeper W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
01-17 00:12:05.046 30417-30417/at.beekeeper W/System.err: at android.app.ActivityThread.access$800(ActivityThread.java:148)
01-17 00:12:05.046 30417-30417/at.beekeeper W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
01-17 00:12:05.046 30417-30417/at.beekeeper W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
01-17 00:12:05.046 30417-30417/at.beekeeper W/System.err: at android.os.Looper.loop(Looper.java:135)
01-17 00:12:05.046 30417-30417/at.beekeeper W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5312)
01-17 00:12:05.046 30417-30417/at.beekeeper W/System.err: at java.lang.reflect.Method.invoke(Native Method)
01-17 00:12:05.046 30417-30417/at.beekeeper W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
01-17 00:12:05.046 30417-30417/at.beekeeper W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
01-17 00:12:05.046 30417-30417/at.beekeeper W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
01-17 00:12:05.046 30417-30417/at.beekeeper W/System.err: Caused by: javax.xml.transform.TransformerException: Attempt to invoke interface method 'org.apache.xml.dtm.DTMAxisTraverser org.apache.xml.dtm.DTM.getAxisTraverser(int)' on a null object reference
01-17 00:12:05.046 30417-30417/at.beekeeper W/System.err: at org.apache.xpath.XPath.execute(XPath.java:365)
01-17 00:12:05.046 30417-30417/at.beekeeper W/System.err: at org.apache.xpath.jaxp.XPathImpl.eval(XPathImpl.java:219)
01-17 00:12:05.046 30417-30417/at.beekeeper W/System.err: at org.apache.xpath.jaxp.XPathImpl.evaluate(XPathImpl.java:281)
01-17 00:12:05.046 30417-30417/at.beekeeper W/System.err: ... 16 more
01-17 00:12:05.046 30417-30417/at.beekeeper W/System.err: Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'org.apache.xml.dtm.DTMAxisTraverser org.apache.xml.dtm.DTM.getAxisTraverser(int)' on a null object reference
01-17 00:12:05.046 30417-30417/at.beekeeper W/System.err: at org.apache.xpath.axes.AxesWalker.setRoot(AxesWalker.java:221)
01-17 00:12:05.046 30417-30417/at.beekeeper W/System.err: at org.apache.xpath.axes.WalkingIterator.setRoot(WalkingIterator.java:157)
01-17 00:12:05.046 30417-30417/at.beekeeper W/System.err: at org.apache.xpath.axes.NodeSequence.setRoot(NodeSequence.java:265)
01-17 00:12:05.046 30417-30417/at.beekeeper W/System.err: at org.apache.xpath.axes.LocPathIterator.execute(LocPathIterator.java:212)
01-17 00:12:05.046 30417-30417/at.beekeeper W/System.err: at org.apache.xpath.XPath.execute(XPath.java:337)
01-17 00:12:05.046 30417-30417/at.beekeeper W/System.err: ... 18 more
答案 0 :(得分:0)
我找到了使用XPath的解决方案。您需要使用InputSource并将XML文件放入/ raw目录中。
InputSource inputSource = new InputSource(context.getResources().openRawResource(R.raw.schedule_default));
XPath xPath = XPathFactory.newInstance().newXPath();
String image;
try {
image = xPath.evaluate("//stage[@token=3]/image/text()", inputSource);
} catch (XPathExpressionException e) {
Log.e(LOG_TAG, "Error evaluating xpath expression.");
e.printStackTrace();
return null;
}
但是我仍然不确定是否可以将XmlPullParser与XPath一起使用...