2

Possible Duplicate:
Read XML file and write to a php array/file

Hi,

I need to update the country list of my website and I want to automate the process. Country list can be found here http://www.iso.org/iso/country_codes...code_lists.htm

I tried it this way – http://www.w3schools.com/php/php_xml_parser_expat.asp (PHP XML Expat Parser) However, this didn't seem to work well as I was confused where to actually 'get' the data and print it to my own array for later use.

Now I want to try it using XML DOM (http://www.php.net/manual/en/book.domxml.php ).

Just want to check with everyone, if I had a simple XML file to read, that contained a country code and country name as follows:

<Entry> 
    <Country_name>AFGHANISTAN</Country_name>
    <Code_element>AF</Code_element>
</Entry>

I want to read this file (DOM method), and then feed the data into a separate file/array of mine that will be accessed by my website. What PHP xml functions would YOU use/recommend to do this simple task?

Any help in this regards is appreciated.

1
  • Please don't post duplicate questions. You had answers on the original. If you don't like those, you should have asked a different question. Commented Feb 24, 2011 at 13:20

2 Answers 2

1

simple + xml =simplexml with dom support too

Sign up to request clarification or add additional context in comments.

Comments

0

For the sample you've provided, here is the code to parse it using DOMDocument.

$countries = array();
$dom = new DOMDocument();
$dom->load('/path/to/file.xml');
foreach ($dom->getElementsByTagName('Entry') as $entry) {
  $country_name = $entry->getElementsByTagName('Country_name')->item(0)->textContent;
  $country_code = $entry->getElementsByTagName('Code_element')->item(0)->textContent;
  $countries[$country_code] = $country_name;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.