xml - PHP How to get SimpleXMLElement Object meta value by attributes name? -


i'm using simplexml_load_file function in php traverse xml file.

i'm stuck in sense i'm trying access value of particulr meta object not sure how so.

this code works, not every xml file same want able have way of getting value subject name.

<?php $file = simplexml_load_file('test.xml');  $subject = $file->head->meta[1][content];  print($subject); ?> 

so can traverse right $subject using $file->head->meta[1][content] said, won't work.

in python, i'd use subject = self.element.find(".//meta[@name='subject']/ @content")

here's example of 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 @ 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 correct keyword</p>         <p style="@body justified">london — britain’s unemployment rate held steady in 3 months through january, reinforcing bank of england’s case keeping interest rates @ record low.</p>         <p style="@body justified">the jobless rate measured international labour organization methods 7.2 percent, same in final quarter of 2013 office national statistics said in london wednesday. that’s in line median forecast in bloomberg survey. jobless claims - narrower measure of unemployment, fell 34,600, more economists had forecast, , wage growth accelerated.</p>         <p style="@body justified">the figures hand ammunition boe governor mark carney’s argument officials should in no hurry increase borrowing costs. rapid falls in unemployment last year forced boe abandon 7 percent threshold considering rate increase. policy makers said last month they’re focused on broader range of measures of spare capacity.</p>       </block>     </body.content>   </body> </nitf> 

you're suing "xpath" in python, available in simplexml.

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

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


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -