0

I am new in xml and data retrieve and i have problem with this code.

XML code:

<?xml version="1.0" encoding="UTF-8"?>
<site>
    <page>
        <content>
            <P>
                <FONT size="2" face="Tahoma">
                    <STRONG>text...</STRONG>
                </FONT>
            </P>
            <P>
                <FONT size="2" face="Tahoma">text....</FONT>
            </P>

            <P align="center">
                <IMG style="WIDTH: 530px" border="1" alt="" src="http://www.alkul.com/online/2014/5/6/child%20disorder.jpg">
            </P>
            <P>
                <STRONG>
                    <FONT size="2" face="Tahoma">text3</FONT>
                </STRONG>
            </P>
            <P>
                <STRONG>
                    <FONT size="2" face="Tahoma">text1</FONT>
                </STRONG>
            </P>
        </content>
    </page>
</site>

php code:

<?php
$html = "";
$url  = "Data.xml";
$xml  = simplexml_load_file($url);    

for ($i = 0; $i<10; $i++) {     
    $title = $xml->page[$i]->content->P->FONT;
    $html .= "<p>$title</p>";
}

echo $html;

I just need to display the content of content node but the output is empty

4
  • 1
    Do you get any error?? Is it problem specific with this node?? Commented Jul 31, 2014 at 18:54
  • no the xml file is good just i cant display the content because its nested i think Commented Jul 31, 2014 at 19:02
  • Have you tried other nested node?? Commented Jul 31, 2014 at 19:03
  • no the xml file is good just i cant display the content because its nested i think Commented Jul 31, 2014 at 19:04

3 Answers 3

2

First of all, the provided XML is not valid as you should receive the following error:

Warning: simplexml_load_string(): Entity: line 8: parser error : Opening and ending tag mismatch: IMG line 8 and P

In XML the IMG element needs to be closed like this:

<IMG style="WIDTH: 530px" border="1" alt="" src="http://www.alkul.com/online/2014/5/6/child%20disorder.jpg"/>

Note the forward slash at the end of the element.
If you do not see that error, please look in your error log or enable error reporting in PHP.

Now the XML can be parsed by SimpleXML. I ended up with this:

$pList = $xml->xpath('./page/content/P');
foreach ($pList as $pElement) {
    $text = strip_tags($pElement->asXML());
    echo $text . "<br>";
}

It selects all the P elements into $pList and iterates over the list. For each element it takes the XML and strips all tags from it, leaving you with just the "inner text" for each element.

Lastly, I'd suggest you use the PHP Simple HTML DOM Parser as it is quite easy to use and more tailored towards scraping data from HTML.

Sign up to request clarification or add additional context in comments.

2 Comments

thank u so much every thing is fine but still i cant display the img tag
@thara I'm not sure what you mean by that. Do you want the src URL to the file? You did not mention this in the question.
1

If you only want to display what is in the content node so here is your code

<?php
$html = "";
$url  = "data.xml";
$xml  = simplexml_load_file($url);

$title = $xml->page->content->asXML();
$html  .= "<p>$title</p>";

echo $html;

Comments

0

You have HTML inside an XML node. This needs XML encoding, normally done with a CDATA block. You then can just use the $xml->page->content element with echo or by casting it to string.

XML (take note of the <![CDATA[ ... ]]> part):

<?xml version="1.0" encoding="UTF-8"?>
<site>
    <page>
        <content><![CDATA[
            <P>
                <FONT size="2" face="Tahoma">
                    <STRONG>text...</STRONG>
                </FONT>
            </P>
            <P>
                <FONT size="2" face="Tahoma">text....</FONT>
            </P>

            <P align="center">
                <IMG style="WIDTH: 530px" border="1" alt="" src="http://www.alkul.com/online/2014/5/6/child%20disorder.jpg">
            </P>
            <P>
                <STRONG>
                    <FONT size="2" face="Tahoma">text3</FONT>
                </STRONG>
            </P>
            <P>
                <STRONG>
                    <FONT size="2" face="Tahoma">text1</FONT>
                </STRONG>
            </P>
        ]]></content>
    </page>
</site>

PHP:

$xml = simplexml_load_file($url);

$firstTenPages = new LimitIterator(new IteratorIterator($xml->page), 0, 10);

foreach ($firstTenPages as $page)
{
    echo $page->content;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.