PHP DOMDocument : How to parse xml/rss Tags with CUSTOM field names? -


i have below rss parse, like:

<?xml version="1.0" encoding="utf-8"?> <rss xmlns:x-wr="http://www.w3.org/2002/12/cal/prod/apple_comp_628d9d8459c556fa#" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:x-example="http://www.example.com/rss/x-example" xmlns:x-microsoft="http://schemas.microsoft.com/x-microsoft" xmlns:xcal="urn:ietf:params:xml:ns:xcal" version="2.0">     <channel>         <item>             <title>about apples</title>             <author>david k. lowie</title>             <x-trumba:customfield name="description">this description apples</xcal:customfield>             <x-trumba:customfield name="category">fruits,food,apple</xcal:customfield>         </item>         <item>             <title>about oranges</title>             <author>marry l. jones</title>             <x-trumba:customfield name="description">this description oranges</xcal:customfield>             <x-trumba:customfield name="category">fruits,food,orange</xcal:customfield>         </item>     </channel> </rss> 

in php, know how read first 2 nodes, like:

$rss = new domdocument(); $rss->load( "http://www.example.com/books.rss" );  foreach( $rss->getelementsbytagname("item") $node ) {     echo $node->getelementsbytagname("title")->item(0)->nodevalue,     echo $node->getelementsbytagname("author")->item(0)->nodevalue, } 

but, these ones problems:

<x-trumba:customfield name="description">this description apples</xcal:customfield> <x-trumba:customfield name="category">fruits,food,apple</xcal:customfield> 

please help:

  • how parse last nodes like <x-trumba:customfield name="description"> ?

(i can't change rss source since it's not under control.)

please kindly help.

you xml invalid, 'x-trumba' prefix not defined, , closing tags of elements use 'xcal' prefix, refering urn:ietf:params:xml:ns:xcal.

so replacing prefix of opening tags 'xcal' , fixing closing tags 'author' makes xml valid.

then possible register xcalendar namespace , use xpath fetch custom field contents:

$rss = new domdocument(); $rss->load( "http://www.example.com/books.rss" ); $xpath = new domxpath($rss); $xpath->registernamespace('x', 'urn:ietf:params:xml:ns:xcal');  foreach( $xpath->evaluate("//item") $item ) {     echo $xpath->evaluate('string(title)', $item), "\n";     echo $xpath->evaluate('string(x:customfield[@name="description"])', $item), "\n"; } 

output:

about apples description apples oranges description oranges 

the xpath expression use condition ([@name="description"]) filter customfield element nodes.


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -