I have realized a really stupid xPath filter in MatLab:
% Construct the DOM.
docNode = xmlread('C:\Users\MATLAB\test.gpx');
% get the xpath mechanism into the workspace
import javax.xml.xpath.*
factory = XPathFactory.newInstance;
xpath = factory.newXPath;
% compile and evaluate the XPath Expression
expression = xpath.compile('gpx/AddressBook/Entry/PhoneNumber');
phoneNumberNode = expression.evaluate(docNode, XPathConstants.NODE);
phoneNumber = phoneNumberNode.getTextContent
With this XML (specifically a .gpx file) it works:
<?xml version='1.0' encoding='ISO-8859-1' standalone='yes' ?>
<gpx version='1.1' creator='TTTracklog V.1.13'>
<AddressBook>
<Entry>
<Name>Friendly J. Mathworker</Name>
<PhoneNumber>(508) 647-7000</PhoneNumber>
<Address hasZip="no" type="work">3 Apple Hill Dr, Natick MA</Address>
</Entry>
</AddressBook>
</gpx>
and text (508) 647-7000 is returned.
Simply adding xmlns attribute to gpx node in this way:
<?xml version='1.0' encoding='ISO-8859-1' standalone='yes' ?>
<gpx version='1.1' creator='TTTracklog V.1.13' xmlns='http://www.topografix.com/GPX/1/1'>
<AddressBook>
<Entry>
<Name>Friendly J. Mathworker</Name>
<PhoneNumber>(508) 647-7000</PhoneNumber>
<Address hasZip="no" type="work">3 Apple Hill Dr, Natick MA</Address>
</Entry>
</AddressBook>
</gpx>
gave me error, and matlab report:
??? Attempt to reference field of non-structure array.
Error in ==> test at 12 phoneNumber = phoneNumberNode.getTextContent
Why? How can I avoid that error?
Dabbler’s comment above is correct. Since you have added a default namespace to your XML document, you must also alter your XPath expression to search for nodes in your new default namespace.
Unqualified names in XPath expressions (like
AddressBook) are in thenullXML namespace, not the default XML namespace of your document.So you want to somehow register your new namespace with your
xpathobject. Like:and then alter your XPath expression to:
There are docs on how to do this with the
javax.xml.xpathAPI here:http://www.ibm.com/developerworks/library/x-javaxpathapi/index.html#N1022D
However, I’m not sure how this translates to Matlab, exactly.