-1

I have a XML file with following schema:

<?xml version="1.0" encoding="UTF-8"?>
<languages>
<language type="persian" abbr_type="fa">
    <menu>
        <home></home>
        <contact></contact>
        <about></about>
        <style></style>
    </menu>
    <title>
        <home></home>
        <contact></contact>
        <about></about>
        <style></style>
        <list></list>
        <biography></biography>
        <picture></picture>
        <movie></movie>
    </title>
    <about></about>
    <welcome></welcome>
</language>
<language type="english" abbr_type="en">
    <menu>
        <home></home>
        <contact></contact>
        <about></about>
        <style></style>
    </menu>
    <title>
        <home></home>
        <contact></contact>
        <about></about>
        <style></style>
        <list></list>
        <biography></biography>
        <picture></picture>
        <movie></movie>
    </title>
    <about></about>
    <welcome></welcome>
</language>

I wanna get Title data if attribute of language tag is "persian".

How can I get a series of data from XML, exactly? Is there any way to get data and put in an array?

Omid Nazifi
  • 5,235
  • 8
  • 30
  • 56
  • http://php.net/manual/en/book.simplexml.php | http://www.w3schools.com/php/php_xml_simplexml.asp – deb0rian Nov 01 '12 at 21:57
  • 1
    possible duplicate of [PHP SimpleXML + Get Attribute](http://stackoverflow.com/questions/10537657/php-simplexml-get-attribute) – Baba Nov 01 '12 at 22:07

1 Answers1

1

Is there any way to get data and put in an array?

Yes, you can use DOMDocument > http://php.net/manual/en/class.domdocument.php . It create tree like structure in which you can easily find what are you looking for.

EXAMPLE

$xml = new DOMDocument();
$xml->loadXML($xmlString);
$xmlNodeArray = $xml->getElementsByTagName('language');
foreach ($xmlNodeArray as $element) {
    if($element->getAttribute('type') == "persian") {
         // do something with that element
    }
}
Nikola Loncar
  • 2,611
  • 1
  • 26
  • 38