2

I'm just messing a bit with XML and XPath with Matlab. I've a pretty complex XML but I just need to access a single Node, and add some other sub-nodes to it.

Here's a part of the XML:

<?xml version="1.0" encoding="UTF-8" ?>
<viper xmlns="http://lamp.cfar.umd.edu/viper#" xmlns:data="http://lamp.cfar.umd.edu/viperdata#">
  <config>
    <descriptor name="Information" type="FILE">
      <attribute dynamic="false" name="SOURCETYPE" type="http://lamp.cfar.umd.edu/viperdata#lvalue">
        <data:lvalue-possibles>
          <data:lvalue-enum value="SEQUENCE" />
          <data:lvalue-enum value="FRAMES" />
        </data:lvalue-possibles>
      </attribute>
      <attribute dynamic="false" name="NUMFRAMES" type="http://lamp.cfar.umd.edu/viperdata#dvalue" />
      <attribute dynamic="false" name="FRAMERATE" type="http://lamp.cfar.umd.edu/viperdata#fvalue" />
      <attribute dynamic="false" name="H-FRAME-SIZE" type="http://lamp.cfar.umd.edu/viperdata#dvalue" />
    </descriptor>
    <descriptor name="PLAYER" type="OBJECT">
      <attribute dynamic="false" name="Name" type="http://lamp.cfar.umd.edu/viperdata#svalue" />
      <attribute dynamic="true" name="Location" type="http://lamp.cfar.umd.edu/viperdata#bbox" />
    </descriptor>
  </config>
  <data>
    <sourcefile filename="C:/Users/XXX/Desktop/ViperGT/mountain.xgtf">
      <file id="0" name="Information">
        <attribute name="SOURCETYPE" />
        <attribute name="NUMFRAMES">
          <data:dvalue value="100" />
        </attribute>
        <attribute name="FRAMERATE">
          <data:fvalue value="1.0" />
        </attribute>
        <attribute name="H-FRAME-SIZE" />
        <attribute name="V-FRAME-SIZE" />
      </file>
      <object framespan="2:99" id="0" name="PLAYER">
        <attribute name="Name">
          <data:svalue value="1" />
        </attribute>
        <attribute name="Location">
          <data:bbox framespan="2:2" height="81" width="182" x="40" y="55" />
        </attribute>
      </object>
    </sourcefile>
  </data>
</viper>

So I've to access to the <data:bbox /> block.

Here's my code:

import javax.xml.xpath.*
factory = XPathFactory.newInstance();
xpath = factory.newXPath();
% compile and evaluate the XPath Expression
expression = xpath.compile('/viper/data/sourcefile/object//data');
% Read XML
gt = xmlread('myGT.xml');
objectNode = expression.evaluate(gt, XPathConstants.NODE);

Any Help?

har07
  • 88,338
  • 12
  • 84
  • 137
Leo91
  • 1,741
  • 3
  • 13
  • 20
  • Google about handling *default namespace* in Java XPath.. – har07 Nov 10 '16 at 01:33
  • @har07 What kind of answer is this? O_o – Leo91 Nov 10 '16 at 09:33
  • It's just a *comment* to be precise... anyway the topic has been asked and answered so many times in various form and context – har07 Nov 10 '16 at 10:13
  • So would you mind to solve my problem? I know the sintax of XPath, I THINK I know what to do, it simply does not work. – Leo91 Nov 10 '16 at 12:34
  • I believe you do know but one crucial thing, which I mentioned earlier: *default namespace*. Element where default namespace is declared, as well as its descendant elements without prefix inherits the default namespace implicitly. You can think of it as if those elements have invisible namespace prefix (if you're familiar with namespace prefix). – har07 Nov 11 '16 at 02:30
  • 1
    I'm just not familiar with Java, so won't attempt to answer this question. You can try to follow this [question](http://stackoverflow.com/questions/3939636/how-to-use-xpath-on-xml-docs-having-default-namespace) among many other questions. – har07 Nov 11 '16 at 02:31
  • 1
    @har07 I've copied and pasted the example from Matlab website :( Btw your link solved my problem. Thanks a lot. :) Please feel free to close and refer to that link (I just have not enough rep) – Leo91 Nov 11 '16 at 08:43

1 Answers1

1

I think you should be using XPathConstants.NODESET instead of XPathConstants.NODE, as you have more then one data element in your xml.

Marcin
  • 215,873
  • 14
  • 235
  • 294
  • Tried, same result. Even if I want to access the first node VIPER, it does not take it :( – Leo91 Nov 10 '16 at 12:36