yodabar
yodabar

Reputation: 4789

PHP DOM get children excluding text nodes

How can I get only child nodes without text nodes? I have the following document:

<Sys>
    <Const>
        <C1>A</C1>
        <C2>B</C2>
        <C3>C</C3>
    </Const>
</Sys>

Getting children of "Const" I get 6 elements,

<?php
$doc = new DOMDocument();
$doc->load($file_path);

$constants = $doc->getElementsByTagName('Const')->item(0);

foreach ($constants->childNodes as $const) {
    echo $const->nodeName . ': ' . $const->nodeValue . '<br />';
}
?>

which prints:

#text:
C1: A
v#text:
C2: B
#text:
C3: C

How can I get the only C1, C2 and C3 nodes, excluding text nodes?

Upvotes: 1

Views: 951

Answers (2)

ThW
ThW

Reputation: 19512

You can use Xpath expressions to fetch specific parts of a DOM.

  • Any "Const" element //Const
  • Child elements of "Const" //Const/*

Example:

$xml = <<<'XML'
<Sys>
    <Const>
        <C1>A</C1>
        <C2>B</C2>
        <C3>C</C3>
    </Const>
</Sys>
XML;

$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMXpath($document);

foreach ($xpath->evaluate('//Const/*') as $constant) {
  echo $constant->nodeName . ': ' . $constant->textContent . "\n";
}

Output:

C1: A
C2: B
C3: C

Upvotes: 1

Nigel Ren
Nigel Ren

Reputation: 57141

In the example given in question, you can simply set the flag preserveWhiteSpace to be false, this prevents the whitespace (new lines, tabs, spaces etc.) from creating extra text nodes...

$doc->preserveWhiteSpace = false;
$doc->load($file_path);

Upvotes: 4

Related Questions