0

Is it possible to read,compare and look for a specefic strings in xml files using xpath from Matlab ? I don't find any documentation.

could someone give me an example ?

<?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> 

here to access to description name="localization"> , I did :

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 

how can I get the value of ?

thanks

0

1 Answer 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.

Sign up to request clarification or add additional context in comments.

1 Comment

hello yuk, I've updated my question by pointing the example of xml file and the code trying to access to nodes , could you help please ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.