0

With this XML:

<?xml version="1.0" encoding="UTF-8" ?>
<databases>
    <default>
        <type>mysql</type>
        <host>localhost</host>
        <table-prefix></table-prefix>
        <username>root</username>
        <password></password>
        <charset>UTF-8</charset>
    </default>
    <test>
        <type>mysql</type>
        <host>localhost</host>
        <table-prefix></table-prefix>
        <username>root</username>
        <password></password>
        <charset>UTF-8</charset>
    </test>
</databases>

Code:

public function get($xpath = '/')
    {
        $dom_object = new DOMDocument();
        $dom_object->load($this->_filename);
        $domxpath_object = new DOMXpath($dom_object);

        $domnodelist_object = $domxpath_object->query($xpath);

        return $this->XMLConfigurationToArray($domnodelist_object);
    }

private function XMLConfigurationToArray(DOMNodeList $domnodelist_object)
    {
        $configuration_array = array();

        foreach ($domnodelist_object as $element)
        {
            if ($element->hasChildNodes())
            {
                foreach ($element->childNodes as $c)
                {
                    print_r('<pre>' . $element->tagName . '</pre>');
                }
            }
        }

        return $configuration_array;
    }

Why it prints out databases 5 times? I call get('/databases') ... Thank you.

5
  • you are expecting to see "databases" two times? Commented Jan 31, 2011 at 19:27
  • databases just once ... is that wrong? Commented Jan 31, 2011 at 19:33
  • 1
    (related) stackoverflow.com/questions/4598409/… Commented Jan 31, 2011 at 19:40
  • If you only want to get <databases> just query / (also see my answer related to the textnodes) Commented Jan 31, 2011 at 19:41
  • 1
    Besides @Gordon precaution, this is a duplicate. Commented Jan 31, 2011 at 19:57

2 Answers 2

1

There are also whitespaces, which are childNodes too (textNodes)

Ignore the textNodes:

if($c->nodeType===1)
{
  echo('<pre>' . $c->tagName . '</pre>');
}

...or use also XPATH to retrieve the child(element)-nodes.

You can also ignore the whitespaces from the start(as described in the topic linked by Gordon):

$dom_object = new DOMDocument();
$dom_object->preserveWhiteSpace=false;
Sign up to request clarification or add additional context in comments.

3 Comments

do you mean tabs == whitespaces? so, in xml, i cannot have whitespaces? will every whitespace be an textnode? thank you.
yes, tabs, linebreaks, spaces. You have one after <databases>, one after </default> and one after </test>
@thomas: No, every text node is going to be a text node. Preserving or stripping white space only text nodes is usually a matter or XML parser configuration. Check for @Gordon's answer
1

Why it prints out databases 5 times? I call get('/databases')

Because the databases top element has 5 children nodes: 2 elements and three (whitespace-only)text nodes, surrounding the elements.

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.