Asked  7 Months ago    Answers:  5   Viewed   36 times

I've never really used the DOM parser before and now I have a question.

How would I go about extracting the URL from this markup:

    <file path="" title="File Name" />



Using simpleXML:

$xml = new SimpleXMLElement($xmlstr);
echo $xml->file['path']."n";

Wednesday, March 31, 2021
answered 7 Months ago

Did you try it? Try this example (Sample: adding data tags).

include 'simple_html_dom.php';

$html_string = '
    <p class="myelems">text inside 1</p>
    <p class="myelems">text inside 2</p>
    <p class="myelems">text inside 3</p>
    <p>simple text 1</p>
    <p>simple text 2</p>

$html = str_get_html($html_string);
foreach($html->find('div p[class="myelems"]') as $key => $p_tags) {
    $p_tags->{'data-index'} = $key;

echo htmlentities($html);


    <p class="myelems" data-index="0">text inside 1</p> 
    <p class="myelems" data-index="1">text inside 2</p> 
    <p class="myelems" data-index="2">text inside 3</p> 
    <p>simple text 1</p> 
    <p>simple text 2</p> 
Friday, May 28, 2021
answered 5 Months ago

The answer is that it doesn't actually need any escaping according to the spec (skipping the mentions of CDATA):

The ampersand character (&) and the left angle bracket (<) must not appear in their literal form (...) If they are needed elsewhere, they must be escaped using either numeric character references or the strings " &amp; " and " &lt; " respectively. The right angle bracket (>) may be represented using the string " &gt; " (...)

To allow attribute values to contain both single and double quotes, the apostrophe or single-quote character (') may be represented as " &apos; ", and the double-quote character (") as " &quot; ".

You can verify this easily by using createTextNode() to perform the correct escaping:

$dom = new DOMDocument;
$e = $dom->createElement('description');
$content = 'single quote: ', double quote: ", opening tag: <, ampersand: &, closing tag: >';
$t = $dom->createTextNode($content);

echo $dom->saveXML();


<?xml version="1.0"?>
<description>single quote: ', double quote: ", opening tag: &lt;, ampersand: &amp;, closing tag: &gt;</description>
Saturday, May 29, 2021
answered 5 Months ago

You need to use Handler to parse the xml. to get attribute value, code is :


Try these links for reference: first and second

Saturday, July 3, 2021
answered 4 Months ago

You need to import any node to append it to another document:

$departmentArray->item($i)->appendChild( $doc->importNode( $employee, true ) );
Friday, August 6, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :