miholzi
miholzi

Reputation: 994

php read xml data with xsd schema

since hours i try to read out the data from a xml file with a xsd schema, but i can not figure it out how i get do the data. i can validate the file but i can not access the data, how can i create an object, do i have to create a new xml file?

thanks!

here is the code so far php

$doc = new DOMDocument();
$doc->load('xml/test.xml');
if ($doc->schemaValidate('http://caaml.org/Schemas/V5.0/Profiles/BulletinEAWS/CAAMLv5_BulletinEAWS.xsd')) {

    echo $doc->MetaData;
}

here the first few lines of the xml file

<?xml version="1.0" encoding="UTF-8"?>
<caaml:Bulletin xmlns:caaml="http://caaml.org/Schemas/V5.0/Profiles/BulletinEAWS" xmlns:gml="http://www.opengis.net/gml" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://caaml.org/Schemas/V5.0/Profiles/BulletinEAWS http://caaml.org/Schemas/V5.0/Profiles/BulletinEAWS/CAAMLv5_BulletinEAWS.xsd" gml:id="BulletinDeID1175">
  <caaml:metaDataProperty>
    <caaml:MetaData>
      <caaml:dateTimeReport>2014-03-06T07:30:00+01:00</caaml:dateTimeReport>
      <caaml:srcRef xlink:href="OpIDLwdTirol"/>
      <caaml:comment>Rudi Mair</caaml:comment>
    </caaml:MetaData>
  </caaml:metaDataProperty>
  <caaml:validTime>
    <caaml:TimeInstant>
      <caaml:timePosition>2014-03-06+01:00</caaml:timePosition>
    </caaml:TimeInstant>
  </caaml:validTime>
  <caaml:bulletinResultsOf>
    <caaml:BulletinMeasurements>
      <caaml:extFiles>
        <caaml:ExtFile gml:id="ExtFileID01">
          <caaml:description></caaml:description>
          <caaml:fileReferenceURI>http://lwdweb.tirol.gv.at/fotos/Franz_Senn_Huette.jpg</caaml:fileReferenceURI>
        </caaml:ExtFile>
      </caaml:extFiles>

Upvotes: 1

Views: 2199

Answers (1)

helderdarocha
helderdarocha

Reputation: 23637

Your code block will only be called if the file is valid. Your example is incomplete. I'm assuming it is complete and valid. That considered, your problem has nothing to do with XML Schema, but with node selection and XML namespaces.

You can extract the data using XPath. You will also need to register a namespace, since your XML source uses one. The code below reads three fields from your source XML:

$doc = new DOMDocument();
$doc->load('xml/test.xml');

$xpath = new DOMXpath($doc);
$xpath->registerNamespace("caaml", "http://caaml.org/Schemas/V5.0/Profiles/BulletinEAWS");

if ($doc->schemaValidate('http://caaml.org/Schemas/V5.0/Profiles/BulletinEAWS/CAAMLv5_BulletinEAWS.xsd')) {

    echo '<ul>'."\n";
    echo '    <li>dateTimeReport: '.$xpath->evaluate("//caaml:MetaData/caaml:dateTimeReport")->item(0)->nodeValue.'</li>'."\n";
    echo '    <li>srcRef: '.$xpath->evaluate("//caaml:MetaData/caaml:srcRef/@*[local-name()='href']")->item(0)->nodeValue.'</li>'."\n";
    echo '    <li>comment: '.$xpath->evaluate("//caaml:MetaData/caaml:comment")->item(0)->nodeValue.'</li>'."\n";
    echo '</ul>'."\n";

}

It will print:

<ul>
    <li>dateTimeReport: 2014-03-06T07:30:00+01:00</li>
    <li>srcRef: OpIDLwdTirol</li>
    <li>comment: Rudi Mair</li>
</ul>

You can see and test it here: http://codepad.viper-7.com/fRBDrU

Upvotes: 2

Related Questions