1

I'm trying to get all xml-files from a folder, which works fine with this code i found on SO here:

$files = glob("folder/*xml");    
if (is_array($files)) {   
    foreach($files as $filename) {
        $xml_file = file_get_contents($filename, FILE_TEXT);
        // and proceed with your code
    }
}

Now I'm trying to get several content out of each xml-file.

By using this after the "// and proceed with your code" part

echo $xml_file.'<br /><br />';

I get the whole content of each file.

But I only want to retrieve several elements and attributes.

The xml-files i'm using are openimmo-based.The structure of these xml-files looks like this (excerpt):

<openimmo>
    <anbieter>
        <anbieternr>12345</anbieternr>
        <firma>company name</firma>
        <immobilie>
            <objektkategorie>
                <objektart>
                    <haus haustyp="DOPPELHAUSHAELFTE"/>
                </objektart>
            </objektkategorie>
            <geo>
                <plz>12345</plz>
            </geo>
        </immobilie>
    </anbieter>
</openimmo>

I use another script, where a singe xml-file gets parsed and i can retrieve it's content like this:

echo 'Zip-Code: '.$user->immobilie->geo->plz.'<br />';

But how can I get the different contents of elements like geo->plz" or the attributes like <haus haustyp="DOPPELHAUSHAELFTE"/> by looping through each file of the given folder?

2 Answers 2

2

What you could do is to convert all xml file into array. Thus you can have multidimensional array ith all the values and then you can crawl through them and get what is needed:

foreach($files as $filename) {
        $xml_file = file_get_contents($filename, FILE_TEXT);      
        $xml = simplexml_load_string($xml_file, "SimpleXMLElement", LIBXML_NOCDATA);
        $json = json_encode($xml);
        $arr = json_decode($json,TRUE);
     }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so far for your help! After a quick test it looks like its working. +1 on that! But I'm gonna have a deeper look at Mohammad's answer, as it looks like I can get into it better...
1

You can use DOMDocument class to parsing xml into DOM. Also use getElementsByTagName() to selecting element based on it name and use nodeValue to get text of element and use getAttribute() to get attribute value.

$doc = new DOMDocument();
$doc->loadXML($xml_file);
$nodeVal = $doc->getElementsByTagName("geo")->item(0)->nodeValue;
// return 12345
$nodeAttr = $doc->getElementsByTagName("haus")->item(0)->getAttribute("haustyp");
// return DOPPELHAUSHAELFTE

So your code should changed to

$doc = new DOMDocument();
foreach($files as $filename) {
    $xml_file = file_get_contents($filename, FILE_TEXT);
    $doc->loadXML($xml_file);
    $nodeVal = $doc->getElementsByTagName("geo")->item(0)->nodeValue;
    $nodeAttr = $doc->getElementsByTagName("haus")->item(0)->getAttribute("haustyp");
}

4 Comments

Thank you too for your help! Your code is working for me, and I'm gonna dive deeper into it to get the output like i need it. But I think I can achieve it from this start.
so far I'm getting it on with your code, except for the "noteAttr/getAttribute"-part. I need to comment this out, otherwise the site is broken and no output at all. Can you see where the problem is? The other part is working fine.
@EMEm Maybe target element doesn't exist in document. Use $doc->getElementsByTagName("haus")->length that return count of selected elements
Thanks again Mohammad! This lead me to the right direction!

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.