douyanti2808 2014-06-06 07:26
浏览 88
已采纳

php循环xml数据与xsd架构 - 如何获取数据

i try to geht the data from a xml file and i have troubles to get data, for example, how can i get the caaml:locRef value or the caaml:beginPosition value?

here is the code so far:

/* a big thank you to helderdarocha */
/* – he already helped me yesterday with a part of this code */ 

$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')) {

        foreach ($xpath->query('//caaml:DangerRating') as $key) {
            echo $key->nodeValue;
            print_r($key);
        }

    } 

and here ist the print_r from $key

DOMElement Object ( [tagName] => caaml:DangerRating [schemaTypeInfo] => [nodeName] => caaml:DangerRating [nodeValue] => 2014-03-03+01:00 2 [nodeType] => 1 [parentNode] => (object value omitted) [childNodes] => (object value omitted) [firstChild] => (object value omitted) [lastChild] => (object value omitted) [previousSibling] => (object value omitted) [nextSibling] => (object value omitted) [attributes] => (object value omitted) [ownerDocument] => (object value omitted) [namespaceURI] => http://caaml.org/Schemas/V5.0/Profiles/BulletinEAWS [prefix] => caaml [localName] => DangerRating [baseURI] => /Applications/MAMP/htdocs/lola/xml/test.xml [textContent] => 2014-03-03+01:00 2 ) 2014-03-04+01:00 2 

and here a part of the xml

 <caaml:DangerRating>
      <caaml:locRef xlink:href="AT7R1"/>
      <caaml:validTime>
        <caaml:TimePeriod>
          <caaml:beginPosition>2014-03-06T00:00:00+01:00</caaml:beginPosition>
          <caaml:endPosition>2014-03-06T11:59:59+01:00</caaml:endPosition>
        </caaml:TimePeriod>
      </caaml:validTime>
      <caaml:validElevation>
        <caaml:ElevationRange uom="m">
          <caaml:beginPosition>2200</caaml:beginPosition>
        </caaml:ElevationRange>
      </caaml:validElevation>
      <caaml:mainValue>2</caaml:mainValue>
    </caaml:DangerRating>
    <caaml:DangerRating>
      <caaml:locRef xlink:href="AT7R1"/>
      <caaml:validTime>
        <caaml:TimePeriod>

thanks!

  • 写回答

1条回答 默认 最新

  • duanban4769 2014-06-06 08:47
    关注

    The second argument of DOMXpath::query()/evaluate() is the context for the expression. You already select and iterate the DangerRating. Inside the loop you can use additional expressions to fetch the data:

    foreach ($xpath->evaluate('//caaml:DangerRating') as $dangerRating) {
      echo $xpath->evaluate(
        'string(caaml:validTime/caaml:TimePeriod/caaml:beginPosition)'
        $dangerRating
      );
    }
    

    The string() Xpath function casts the first node of the location path into a string. This way you don't need the ->item(0)->nodeValue in PHP. It will only work with evaluate().

    The caaml:locRef value is stored in an attribute in a different namespace. So you will need to register that namespace first:

    $xpath->registerNamespace("xlink", "http://www.w3.org/1999/xlink");
    

    After that you can fetch and cast the attribute node:

    $locRef = $xpath->evaluate(
     'string(caaml:locRef/@xlink:href)', $dangerRating
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line