How to skip reading of a content of a particular tag in xml file using php?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kaushal Elsner
    New Member
    • Jan 2011
    • 23

    How to skip reading of a content of a particular tag in xml file using php?

    Suppose this is an XML file.
    Code:
    <subject>Inquiry</subject>
    <message>
     <![CDATA[
       <div>Hi</div>
       <div>Hello</div>
       <div>How Are You</div>
     ]]>
    </message>
    My logic to read this xml file using php is...
    Code:
    $xml = simplexml_load_file("xml_mail_format2.xml");
    foreach($xml->children() as $child)
    {
      if($child->getName() == "message")
      {
        foreach($child->children() as $children)
        {
          $message = $children;
        }
      }
    }
    Now the problem is, The whole message tag is being read by this. I want only "Hello" to be read. What should be done?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    either don’t use CDATA or extract the CDATA text and parse that again.

    Comment

    • Kaushal Elsner
      New Member
      • Jan 2011
      • 23

      #3
      I don't know how to do it because i am new to XML but can you write the things which exactly i want.(That to just read "Hello")ins tead of Hi Hello How are you. Please do something.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        you already have (and use) the tools you need, though it seems you need a better understanding of the DOM.

        when your XML source code is read into the DOM, an object tree is constructed. in your case the tree (parts of it) looks like (omitting the attributes of the objects).

        Code:
        |-- (DOMElement) <subject>
        |     |-- (DOMText) "Inquiry"
        |
        |-- (DOMElement) <message>
              |-- (DOMText) "\n "
              |-- (DOMCharacterData) "\n   <div>Hi</div>\n   <div>Hello</div>\n   <div>How Are You</div>\n "
              |-- (DOMText) "\n"
        what you need to do is extract the text which holds the "inline XML" and repeat XML parsing on that.
        Last edited by Dormilich; Feb 9 '11, 10:15 AM.

        Comment

        Working...