0

I would need to parse an XML file which contains a CDATA tag. Inside this tag, there is another tag that I want to get. How can I achieve this by using XMLReader?

Example:

<glz:Param name="TITLE">
       <![CDATA[Yellow <http://www.yellow.it>]]>
</glz:Param>

How can I get the whole info Yellow <http://www.yellow.it>? I can only get 'Yellow'.

This is my code:

// load file, create a reader variable, etc.
if($reader->nodeType == XMLReader::CDATA)
{
   echo $reader->value;
}
6
  • are you echo'ing the value in a browser or a shell? Commented Jul 10, 2018 at 14:36
  • @Gordon browser Commented Jul 10, 2018 at 14:41
  • 1
    ok, so the issue is likely that XmlReader correctly fetches the entire content in the CDATA tag, but your browser inteprets it as html again. Check the page source to see if it contains the a element. If so, try echo htmlentities($reader->value) or send a header with content-type: text/plain Commented Jul 10, 2018 at 15:29
  • @Gordon it worked out! Thank you so much! :) Commented Jul 11, 2018 at 6:56
  • @Gordon maybe you should write it as answer instead of comment ... so that I can choose it as best answer :) Commented Jul 11, 2018 at 6:57

2 Answers 2

1

As per your comments:

The issue is likely that XmlReader correctly fetches the entire content in the CDATA tag, but your browser inteprets it as html again. Check the page source to see if it contains the a element. If so, try

echo htmlentities($reader->value); 

or send a header with content-type: text/plain.

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

Comments

0

You can get it by string search. As you can see, String "http://www.yellow.it]]>"is not XML so that you can not parse it using XMLReader. Please search string on it. For example, you can split the string as "http:" and you can get 2 sub strings. From second String, you can get the full link without ">]]>".

Hope it will be helpful.

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.