我正在使用一些使用xpath类的脚本。在Octlab中没有实现Matlab中等效的 import 函数。我在这里http://undocumentedmatlab.com/blog/java-class-access-pitfalls发现Matlab无法使用非公共构造函数创建对象。我认为它在Octave中类似。所以行
javaObject("javax.xml.xpath.XPathFactory")
error: [java] java.lang.NoSuchMethodException: javax.xml.xpath.XPathFactory
给出错误。但是,有一个公共静态类方法返回类引用 newInstance ,因此可以创建一个类。 XPathConstans 类没有类似的方法。有没有办法让这个类在Octave中可以访问?
javaaddpath("C:/Program Files/Java/jdk1.8.0_60/jre/lib/rt.jar");
% Construct the DOM.
% These 3 lines are equivalent to xDoc = xmlread(filename) in matlab
parser = javaObject("com.sun.org.apache.xerces.internal.parsers.DOMParser");
parser.parse(filename);
xDoc = parser.getDocument;
% Create an XPath expression.
%factory = javaObject("javax.xml.xpath.XPathFactory");
factory = javaMethod("newInstance","javax.xml.xpath.XPathFactory")
xpath = factory.newXPath
expression = xpath.compile('//demosection/label');
% Apply the expression to the DOM.
%constants = javaObject("javax.xml.xpath.XPathConstants")
nodeList = expression.evaluate(xDoc,XPathConstants.NODESET);
% Iterate through the nodes that are returned.
for i = 1:nodeList.getLength
node = nodeList.item(i-1);
disp(char(node.getFirstChild.getNodeValue))
end
答案 0 :(得分:0)
您正在尝试访问f
类的静态字段。要访问字段,请使用XPathConstants
。由于该类没有构造,因此您需要将其与类名一起使用(而不是类的实例):
java_get()