3

I am trying to display Xml content in to tables, all works perfectly but some content in the tag that i don't want to display, I want only image but not

November 2012 calendar from 5.10 The Test

like in xml,
 <content:encoded><![CDATA[<p>November 2012 calendar from 5.10 The Test</p>
    <p><a class="shutterset_" href='http://trance-gemini.com/wordpress/wp-content/gallery/calendars/laura-bertram-trance-gemini-145-1080.jpg' title='&lt;br&gt;November 2012 calendar from 5.10 The Test&lt;br&gt; &lt;a href=&quot;</a></p>]]>
</content:encoded> 

I want to display image but not

November 2012 calendar from 5.10 The Test

.
<?php
// load SimpleXML
$item = new SimpleXMLElement('test1.xml', null, true);

echo <<<EOF
<table border="1px">
        <tr cl>

        </tr>       
EOF;
foreach($item->channel->item as $boo) // loop through our books
{
        echo <<<EOF

         <tr>
            <td rowspan="3">{$boo->children('content', true)->encoded}</td>
            <td>{$boo->title}</td>   
        </tr>

        <tr>
           <td>{$boo->description}</td>
        </tr>

        <tr>
           <td>{boo->comments}</td>
        </tr>
EOF;
}
echo '</table>';
?>
hakre
  • 193,403
  • 52
  • 435
  • 836
user2249819
  • 33
  • 1
  • 1
  • 6
  • 2
    "contents in thi tag that i dant eanto display" ??? A little more effort in writing a good question would help. – str Apr 06 '13 at 09:33
  • possible duplicate of [PHP DOMDocument getting Attribute of Tag](http://stackoverflow.com/questions/1597746/php-domdocument-getting-attribute-of-tag) - you might not understand why I suggest this duplicate, please see my answer below: http://stackoverflow.com/a/15850774/367456 – hakre Apr 06 '13 at 12:12
  • Another possible duplicate: [How to get an attribute with SimpleXML?](http://stackoverflow.com/q/3410520/367456) – hakre Apr 06 '13 at 12:46
  • Your question itself gave me a solution - using the proper reference for the field. Thanks. – Sri Aug 10 '15 at 06:36

2 Answers2

7

I once answered it but I don't find the answer any longer.

If you take a look at the string (simplified/beautified):

<content:encoded><![CDATA[
    <p>Lorem Ipsom</p>
    <p>
      <a href='laura-bertram-trance-gemini-145-1080.jpg' 
         title='&lt;br&gt;November 2012 calendar from 5.10 The Test&lt;br&gt; &lt;a href=&quot;</a>
    </p>]]>
</content:encoded> 

You can see that you have HTML encoded inside the node-value of the <content:encoded> element. So first you need to obtain the HTML value, which you already do:

$html = $boo->children('content', true)->encoded;

Then you need to parse the HTML inside $html. With which libraries HTML parsing can be done with PHP is outlined in:

If you decide to use the more or less recommended DOMDocument for the job, you only need to get the attribute value of a certain element:

Or for its sister library SimpleXML you already use (so this is more recommended, see as well the next section):


In context of your question here the following tip:

You're using SimpleXML. DOMDocument is a sister-library, meaning you can interchange between the two so you don't need to learn a full new library.

For example, you can use only the HTML parsing feature of DOMDocument, but import it then into SimpleXML. This is useful, because SimpleXML does not support HTML parsing.

That works via simplexml_import_dom().

A simplified step-by-step example:

// get the HTML string out of the feed:
$htmlString = $boo->children('content', true)->encoded;

// create DOMDocument for HTML parsing:
$htmlParser = new DOMDocument();

// load the HTML:
$htmlParser->loadHTML($htmlString);

// import it into simplexml:
$html = simplexml_import_dom($htmlParser);

Now you can use $html as a new SimpleXMLElement that represents the HTML document. As your HTML chunks did not have any <body> tags, according to the HTML specification, they are put inside the <body> tag. This will allow you for example to access the href attribute of the first <a> inside the second <p> element in your example:#

// access the element you're looking for:
$href = $html->body->p[1]->a['href'];

Here the full view from above (Online Demo):

// get the HTML string out of the feed:
$htmlString = $boo->children('content', true)->encoded;

// create DOMDocument for HTML parsing:
$htmlParser = new DOMDocument();

// your HTML gives parser warnings, keep them internal:
libxml_use_internal_errors(true);

// load the HTML:
$htmlParser->loadHTML($htmlString);

// import it into simplexml:
$html = simplexml_import_dom($htmlParser);

// access the element you're looking for:
$href = $html->body->p[1]->a['href'];

// output it
echo $href, "\n";

And what it outputs:

laura-bertram-trance-gemini-145-1080.jpg
Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • 2
    I'd add just one thing: for sanity later, add an explicit `(string)` cast in `$htmlString = (string)$boo ...` and `$href = (string)$html ...`. I find the mental overhead of always adding it when not needed is less than the overhead of debugging those cases where it was needed but you didn't realise. – IMSoP Apr 07 '13 at 22:44
  • @IMSoP: You never need it in something called string context in PHP. It's good to know about it even apart from SimpleXML alone. E.g. with `echo` it's always not necessary. – hakre Apr 07 '13 at 23:40
  • @hakre :this is work when i run on html file,but this code i applied to in my php file i am getting error in-> notice: Undefined variable: htmlString in E:\Work Space\xampp\htdocs\Xml-Paser\note1.php on line 20 Notice: Undefined variable: htmlParser in E:\WorkSpace\xampp\htdocs\Xml-Paser\note1.php on line 30 – user2249819 Apr 08 '13 at 05:19
  • @user2249819: Insert it at the right place in your file. As you can see with my example, I don't have any loop: http://eval.in/15171 – hakre Apr 08 '13 at 07:27
  • @hakre Yes, I know. But like I said, I find it easier to always cast than remember which functions will do it automatically. Assigning to a variable can be especially problematic, as a non-string context call could happen dozens of lines later. – IMSoP Apr 08 '13 at 07:47
  • Sure, both ways are applicable. I normally don't care until I know I need string values. Then I can turn them into those. But the magic with SimpleXML is cause of many questions on SO, so it's good to educate this. – hakre Apr 08 '13 at 07:53
  • can any of write code fore me ,i am getting error ,which place i want to change? – user2249819 Apr 08 '13 at 11:12
  • 1
    @user2249819: Yes you can hire me for that. – hakre Apr 08 '13 at 11:14
  • ha ha ,dear i m a student of engg,not a developer ,ok i just want tu learn here , – user2249819 Apr 09 '13 at 11:07
  • @ hakre if want to really help me as my teacher then please,other wise thanx to response me Sir. – user2249819 Apr 09 '13 at 12:09
-3

you would need to parse the image url eg via preg_match and this regex '(http://(?:[^']*))'

Valerij
  • 27,090
  • 1
  • 26
  • 42
  • Thanx Vprimachenko,but can you tell me in which place i put this,can you give me one exemple or can you tell me in which place i have to change – user2249819 Apr 06 '13 at 10:05