Question

I'm using the simplexml_load_file function in PHP to traverse an xml file.

I'm stuck in the sense that I'm trying to access the value of a particulr meta object but am not sure how to do so.

This code works, but not every xml file will be the same so I want to be able to have some way of getting the value for the subject by name.

<?php
$file = simplexml_load_file('test.xml');

$subject = $file->head->meta[1][content];

print($subject);
?>

So basically I can traverse right to the $subject by using $file->head->meta[1][content] but like I said, this won't always work.

In Python, I'd use something like subject = self.element.find(".//meta[@name='subject']/ @content")

Here's an example of some xml I'm parsing:

<?xml version="1.0" encoding="windows-1252"?>
<nitf>
  <head>
    <meta name="ProgramVersion" content="2.0"/>
    <meta name="subject" content="XML test nonjump"/>
    <meta name="Category" content="Business"/>
    <meta name="Priority" content="Inside"/>
    <meta name="Format" content="3cB"/>
    <meta name="AuthorID" content="532944370"/>
    <ads/>
    <docdata>
      <correction id-string="401"/>
      <urgency ed-urg="7"/>
      <date.release norm="2014-03-21T00:01:00-07:00"/>
      <doc.copyright year="2014" holder="APWire"/>
      <date.expire norm="2014-04-18T12:01:00-07:00"/>
      <identified-content/>
    </docdata>
  </head>
  <body>
    <body.head>
      <hedline>
        <hl1 style="@Hed Benton">Test: British unemployment stays at 7.2 percent reinforcing BOE guidance message</hl1>
      </hedline>
      <byline>
        <p style="@Byline">By Test User</p>
        <p style="@Byline2">Bloomberg News</p>
      </byline>
    </body.head>
    <body.content>
      <block>
        <p style="@Subhed 1 col" lede="true">Repeating to correct keyword</p>
        <p style="@Body justified">LONDON — Britain’s unemployment rate held steady in the three months through January, reinforcing the Bank of England’s case for keeping interest rates at a record low.</p>
        <p style="@Body justified">The jobless rate measured by International Labour Organization methods was 7.2 percent, the same as in the final quarter of 2013 the Office for National Statistics said in London Wednesday. That’s in line with the median forecast in a Bloomberg survey. Jobless claims - a narrower measure of unemployment, fell 34,600, more than economists had forecast, and wage growth accelerated.</p>
        <p style="@Body justified">The figures hand ammunition to BOE Governor Mark Carney’s argument that officials should be in no hurry to increase borrowing costs. Rapid falls in unemployment last year forced the BOE to abandon the 7 percent threshold for considering a rate increase. Policy makers said last month they’re now focused on a broader range of measures of spare capacity.</p>
      </block>
    </body.content>
  </body>
</nitf>
Was it helpful?

Solution

You're suing "Xpath" in python, which is also available in SimpleXML.

$result = $element->xpath(".//meta[@name='subject']/ @content"); 

Documentation is here: http://www.php.net/manual/en/simplexmlelement.xpath.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top