Search code examples
javascriptxmlcdata

Javascript querySelector CDATA in xml file


I have this path of my xml file:

<description>
    <![CDATA[<p class="image"><img width="250" height="83" src="http://www.mydomain.com/picture/pic01.png" class="post-image" alt="Post 01 Image" title="The Demo Post" /></p>My Content
    ]]>
</description>

How can i select the src atribute of tag img in CDATA using querySelector (Javascript).

Thanks!


Solution

  • var description = xmldoc.getElementsByTagName("description")[0],
        html = description.innerText || description.textContent,
        root = document.createElement("div"),
        imgsrc;
    
    root.innerHTML = html;
    imgsrc = root.querySelector("img").src;