register a namespace in xPath filter

7 次查看(过去 30 天)
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'>
<Entry>
<Name>Friendly J. Mathworker</Name>
<PhoneNumber>(508) 647-7000</PhoneNumber>
<Address hasZip="no" type="work">3 Apple Hill Dr, Natick MA</Address>
</Entry>
</gpx>
but as soon add a namespace to this file I get in trouble. For example, this xml doesn't works:
<?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' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd'>
<Entry>
<Name>Friendly J. Mathworker</Name>
<PhoneNumber>(508) 647-7000</PhoneNumber>
<Address hasZip="no" type="work">3 Apple Hill Dr, Natick MA</Address>
</Entry>
</gpx>
My filter broke up, with second XML file, saying that I'm attempting to reference field of non-structure array. How can I register the default namespace?
  2 个评论
sst
sst 2013-11-4
编辑:sst 2013-11-4
Hello marianoc84, I am also looking for a solution to a similar problem. Do let me know if you find anything.
Jarrod Rivituso
Jarrod Rivituso 2013-11-7
I'm not sure how do do this without really getting into the Java files. :)
There's a tool I put together which allows you to extract data via XPath with namespace support.
You may find it useful!

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structured Data and XML Documents 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by