gpt4 book ai didi

java - 评估 XPath 和 XmlResourceParser 会导致异常

转载 作者:太空宇宙 更新时间:2023-11-04 13:02:14 25 4
gpt4 key购买 nike

我的资源中有一个 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>

我想获取特定阶段的图像元素的文本(通过 token )。我的方法是这样的:

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

最佳答案

我找到了使用 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 一起使用...

关于java - 评估 XPath 和 XmlResourceParser 会导致异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34833310/

25 4 0
文章推荐: html - 使
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com