register a namespace in xPath filter
7 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Jarrod Rivituso
il 7 Nov 2013
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!
Risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!