0

是否可以使用 Matlab 中的 xpath 读取、比较和查找 xml 文件中的特定字符串?我没有找到任何文档。

有人可以给我一个例子吗?

<?xml version="1.0" encoding="UTF-8"?> 
<address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation='test.xsd'>
 <lists name="myState">
         <description name="-temp">-20</description>
         <description name="localization">north</description>  
         <description name="-state">false</description> 
  </lists>
</address>  
<language language="english" name=""> 
     <description name="population">5000</description> 
</language> 

在这里访问 description name="localization"> ,我做了:

docNode = xmlread(myXMLFILE);
factory = XPathFactory.newInstance;
xpath = factory.newXPath;

% compile and evaluate the XPath Expression
 expression = xpath.compile(adress/lists/description')
description = expression.evaluate(docNode, XPathConstants.NODE);
descriptionValue = phoneNumberNode.getTextContent  % this gives me -20 

我怎样才能得到价值?

谢谢

4

1 回答 1

1

Have you tried Google? One of the first link gave me a good example of using XPath on FileExchange:

Using XPath from MATLAB

An XPath package started shipping as part of Java 5, so we can use it from MATLAB. This is a simple example.

The Java XPath API tutorial on ibm.com is a good introduction to XPath in Java.

% Import the XPath classes
import javax.xml.xpath.*

% Construct the DOM.
doc = xmlread(which('demos/demos.xml'));

% Create an XPath expression.
factory = XPathFactory.newInstance;
xpath = factory.newXPath;
expression = xpath.compile('//demosection/label');

% Apply the expression to the DOM.
nodeList = expression.evaluate(doc,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

Another good article is in Mike's blog - XML and MATLAB: Navigating a Tree. It has a part specifically on using XPath.

于 2012-04-20T16:38:37.537 回答