Filter löschen
Filter löschen

register a namespace in xPath filter

6 Ansichten (letzte 30 Tage)
marianoc84
marianoc84 am 15 Okt. 2011
Kommentiert: Jarrod Rivituso am 7 Nov. 2013
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 Kommentare
sst
sst am 4 Nov. 2013
Bearbeitet: sst am 4 Nov. 2013
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 am 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!

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by