Skip to content

Instantly share code, notes, and snippets.

@jtarleton
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtarleton/ee0375efb8eaeb53c0a4 to your computer and use it in GitHub Desktop.
Save jtarleton/ee0375efb8eaeb53c0a4 to your computer and use it in GitHub Desktop.
XML Parsing in PHP Tutorial
<?xml version="1.0" encoding="utf-8" ?>
<countries>
<code_country>
<country_id>1</country_id>
<country>United States</country>
<country_code>USA</country_code>
</code_country>
<code_country>
<country_id>2</country_id>
<country>Canada</country>
<country_code>CAN</country_code>
</code_country>
<code_country>
<country_id>3</country_id>
<country>Mexico</country>
<country_code>MEX</country_code>
</code_country>
<code_country>
<country_id>4</country_id>
<country>Ireland</country>
<country_code>IRL</country_code>
</code_country>
<code_country>
<country_id>5</country_id>
<country>United Kingdom</country>
<country_code>GBR</country_code>
</code_country>
<code_country>
<country_id>6</country_id>
<country>France</country>
<country_code>FRA</country_code>
</code_country>
<code_country>
<country_id>7</country_id>
<country>Germany</country>
<country_code>DEU</country_code>
</code_country>
<code_country>
<country_id>8</country_id>
<country>Spain</country>
<country_code>ESP</country_code>
</code_country>
<code_country>
<country_id>9</country_id>
<country>Italy</country>
<country_code>ITA</country_code>
</code_country>
<code_country>
<country_id>504</country_id>
<country>Angola</country>
<country_code>AGO</country_code>
</code_country>
<code_country>
<country_id>505</country_id>
<country>Anguilla</country>
<country_code>AIA</country_code>
</code_country>
<code_country>
<country_id>506</country_id>
<country>Aland Islands</country>
<country_code>ALA</country_code>
</code_country>
<code_country>
<country_id>507</country_id>
<country>Albania</country>
<country_code>ALB</country_code>
</code_country>
<code_country>
<country_id>508</country_id>
<country>Andorra</country>
<country_code>AND</country_code>
</code_country>
<code_country>
<country_id>509</country_id>
<country>United Arab Emirates</country>
<country_code>ARE</country_code>
</code_country>
<code_country>
<country_id>510</country_id>
<country>Argentina</country>
<country_code>ARG</country_code>
</code_country>
<code_country>
<country_id>511</country_id>
<country>Armenia</country>
<country_code>ARM</country_code>
</code_country>
<code_country>
<country_id>512</country_id>
<country>American Samoa</country>
<country_code>ASM</country_code>
</code_country>
<code_country>
<country_id>513</country_id>
<country>Antarctica</country>
<country_code>ATA</country_code>
</code_country>
<code_country>
<country_id>514</country_id>
<country>French Southern Territories</country>
<country_code>ATF</country_code>
</code_country>
<code_country>
<country_id>515</country_id>
<country>Antigua and Barbuda</country>
<country_code>ATG</country_code>
</code_country>
<code_country>
<country_id>516</country_id>
<country>Australia</country>
<country_code>AUS</country_code>
</code_country>
<code_country>
<country_id>517</country_id>
<country>Austria</country>
<country_code>AUT</country_code>
</code_country>
<code_country>
<country_id>518</country_id>
<country>Azerbaijan</country>
<country_code>AZE</country_code>
</code_country>
<code_country>
<country_id>519</country_id>
<country>Burundi</country>
<country_code>BDI</country_code>
</code_country>
<code_country>
<country_id>520</country_id>
<country>Belgium</country>
<country_code>BEL</country_code>
</code_country>
<code_country>
<country_id>521</country_id>
<country>Benin</country>
<country_code>BEN</country_code>
</code_country>
<code_country>
<country_id>522</country_id>
<country>Bonaire, Sint Eustatius and Saba</country>
<country_code>BES</country_code>
</code_country>
<code_country>
<country_id>523</country_id>
<country>Burkina Faso</country>
<country_code>BFA</country_code>
</code_country>
<code_country>
<country_id>524</country_id>
<country>Bangladesh</country>
<country_code>BGD</country_code>
</code_country>
<code_country>
<country_id>525</country_id>
<country>Bulgaria</country>
<country_code>BGR</country_code>
</code_country>
<code_country>
<country_id>526</country_id>
<country>Bahrain</country>
<country_code>BHR</country_code>
</code_country>
<code_country>
<country_id>527</country_id>
<country>Bahamas</country>
<country_code>BHS</country_code>
</code_country>
<code_country>
<country_id>528</country_id>
<country>Bosnia and Herzegovina</country>
<country_code>BIH</country_code>
</code_country>
<code_country>
<country_id>529</country_id>
<country>Saint Barthélemy</country>
<country_code>BLM</country_code>
</code_country>
<code_country>
<country_id>530</country_id>
<country>Belarus</country>
<country_code>BLR</country_code>
</code_country>
<code_country>
<country_id>531</country_id>
<country>Belize</country>
<country_code>BLZ</country_code>
</code_country>
<code_country>
<country_id>532</country_id>
<country>Bermuda</country>
<country_code>BMU</country_code>
</code_country>
<code_country>
<country_id>533</country_id>
<country>Bolivia, Plurinational State of</country>
<country_code>BOL</country_code>
</code_country>
<code_country>
<country_id>534</country_id>
<country>Brazil</country>
<country_code>BRA</country_code>
</code_country>
<code_country>
<country_id>535</country_id>
<country>Barbados</country>
<country_code>BRB</country_code>
</code_country>
<code_country>
<country_id>536</country_id>
<country>Brunei Darussalam</country>
<country_code>BRN</country_code>
</code_country>
<code_country>
<country_id>537</country_id>
<country>Bhutan</country>
<country_code>BTN</country_code>
</code_country>
<code_country>
<country_id>538</country_id>
<country>Bouvet Island</country>
<country_code>BVT</country_code>
</code_country>
<code_country>
<country_id>539</country_id>
<country>Botswana</country>
<country_code>BWA</country_code>
</code_country>
<code_country>
<country_id>540</country_id>
<country>Central African Republic</country>
<country_code>CAF</country_code>
</code_country>
<code_country>
<country_id>542</country_id>
<country>Cocos (Keeling) Islands</country>
<country_code>CCK</country_code>
</code_country>
<code_country>
<country_id>543</country_id>
<country>Switzerland</country>
<country_code>CHE</country_code>
</code_country>
<code_country>
<country_id>544</country_id>
<country>Chile</country>
<country_code>CHL</country_code>
</code_country>
<code_country>
<country_id>545</country_id>
<country>China</country>
<country_code>CHN</country_code>
</code_country>
<code_country>
<country_id>546</country_id>
<country>Côte d'Ivoire</country>
<country_code>CIV</country_code>
</code_country>
<code_country>
<country_id>547</country_id>
<country>Cameroon</country>
<country_code>CMR</country_code>
</code_country>
<code_country>
<country_id>548</country_id>
<country>Congo, the Democratic Republic of the</country>
<country_code>COD</country_code>
</code_country>
<code_country>
<country_id>549</country_id>
<country>Congo</country>
<country_code>COG</country_code>
</code_country>
<code_country>
<country_id>550</country_id>
<country>Cook Islands</country>
<country_code>COK</country_code>
</code_country>
<code_country>
<country_id>551</country_id>
<country>Colombia</country>
<country_code>COL</country_code>
</code_country>
<code_country>
<country_id>552</country_id>
<country>Comoros</country>
<country_code>COM</country_code>
</code_country>
<code_country>
<country_id>553</country_id>
<country>Cape Verde</country>
<country_code>CPV</country_code>
</code_country>
<code_country>
<country_id>554</country_id>
<country>Costa Rica</country>
<country_code>CRI</country_code>
</code_country>
<code_country>
<country_id>555</country_id>
<country>Cuba</country>
<country_code>CUB</country_code>
</code_country>
<code_country>
<country_id>556</country_id>
<country>Curaçao</country>
<country_code>CUW</country_code>
</code_country>
<code_country>
<country_id>557</country_id>
<country>Christmas Island</country>
<country_code>CXR</country_code>
</code_country>
<code_country>
<country_id>558</country_id>
<country>Cayman Islands</country>
<country_code>CYM</country_code>
</code_country>
<code_country>
<country_id>559</country_id>
<country>Cyprus</country>
<country_code>CYP</country_code>
</code_country>
<code_country>
<country_id>560</country_id>
<country>Czech Republic</country>
<country_code>CZE</country_code>
</code_country>
<code_country>
<country_id>562</country_id>
<country>Djibouti</country>
<country_code>DJI</country_code>
</code_country>
<code_country>
<country_id>563</country_id>
<country>Dominica</country>
<country_code>DMA</country_code>
</code_country>
<code_country>
<country_id>564</country_id>
<country>Denmark</country>
<country_code>DNK</country_code>
</code_country>
<code_country>
<country_id>565</country_id>
<country>Dominican Republic</country>
<country_code>DOM</country_code>
</code_country>
<code_country>
<country_id>566</country_id>
<country>Algeria</country>
<country_code>DZA</country_code>
</code_country>
<code_country>
<country_id>567</country_id>
<country>Ecuador</country>
<country_code>ECU</country_code>
</code_country>
<code_country>
<country_id>568</country_id>
<country>Egypt</country>
<country_code>EGY</country_code>
</code_country>
<code_country>
<country_id>569</country_id>
<country>Eritrea</country>
<country_code>ERI</country_code>
</code_country>
<code_country>
<country_id>570</country_id>
<country>Western Sahara</country>
<country_code>ESH</country_code>
</code_country>
<code_country>
<country_id>572</country_id>
<country>Estonia</country>
<country_code>EST</country_code>
</code_country>
<code_country>
<country_id>573</country_id>
<country>Ethiopia</country>
<country_code>ETH</country_code>
</code_country>
<code_country>
<country_id>574</country_id>
<country>Finland</country>
<country_code>FIN</country_code>
</code_country>
<code_country>
<country_id>575</country_id>
<country>Fiji</country>
<country_code>FJI</country_code>
</code_country>
<code_country>
<country_id>576</country_id>
<country>Falkland Islands (Malvinas)</country>
<country_code>FLK</country_code>
</code_country>
<code_country>
<country_id>578</country_id>
<country>Faroe Islands</country>
<country_code>FRO</country_code>
</code_country>
<code_country>
<country_id>579</country_id>
<country>Micronesia, Federated States of</country>
<country_code>FSM</country_code>
</code_country>
<code_country>
<country_id>580</country_id>
<country>Gabon</country>
<country_code>GAB</country_code>
</code_country>
<code_country>
<country_id>582</country_id>
<country>Georgia</country>
<country_code>GEO</country_code>
</code_country>
<code_country>
<country_id>583</country_id>
<country>Guernsey</country>
<country_code>GGY</country_code>
</code_country>
<code_country>
<country_id>584</country_id>
<country>Ghana</country>
<country_code>GHA</country_code>
</code_country>
<code_country>
<country_id>585</country_id>
<country>Gibraltar</country>
<country_code>GIB</country_code>
</code_country>
<code_country>
<country_id>586</country_id>
<country>Guinea</country>
<country_code>GIN</country_code>
</code_country>
<code_country>
<country_id>587</country_id>
<country>Guadeloupe</country>
<country_code>GLP</country_code>
</code_country>
<code_country>
<country_id>588</country_id>
<country>Gambia</country>
<country_code>GMB</country_code>
</code_country>
<code_country>
<country_id>589</country_id>
<country>Guinea-Bissau</country>
<country_code>GNB</country_code>
</code_country>
<code_country>
<country_id>590</country_id>
<country>Equatorial Guinea</country>
<country_code>GNQ</country_code>
</code_country>
<code_country>
<country_id>591</country_id>
<country>Greece</country>
<country_code>GRC</country_code>
</code_country>
<code_country>
<country_id>592</country_id>
<country>Grenada</country>
<country_code>GRD</country_code>
</code_country>
<code_country>
<country_id>593</country_id>
<country>Greenland</country>
<country_code>GRL</country_code>
</code_country>
<code_country>
<country_id>594</country_id>
<country>Guatemala</country>
<country_code>GTM</country_code>
</code_country>
<code_country>
<country_id>595</country_id>
<country>French Guiana</country>
<country_code>GUF</country_code>
</code_country>
<code_country>
<country_id>596</country_id>
<country>Guam</country>
<country_code>GUM</country_code>
</code_country>
<code_country>
<country_id>597</country_id>
<country>Guyana</country>
<country_code>GUY</country_code>
</code_country>
<code_country>
<country_id>598</country_id>
<country>Hong Kong</country>
<country_code>HKG</country_code>
</code_country>
<code_country>
<country_id>599</country_id>
<country>Heard Island and McDonald Islands</country>
<country_code>HMD</country_code>
</code_country>
<code_country>
<country_id>600</country_id>
<country>Honduras</country>
<country_code>HND</country_code>
</code_country>
<code_country>
<country_id>601</country_id>
<country>Croatia</country>
<country_code>HRV</country_code>
</code_country>
<code_country>
<country_id>602</country_id>
<country>Haiti</country>
<country_code>HTI</country_code>
</code_country>
<code_country>
<country_id>603</country_id>
<country>Hungary</country>
<country_code>HUN</country_code>
</code_country>
<code_country>
<country_id>604</country_id>
<country>Indonesia</country>
<country_code>IDN</country_code>
</code_country>
<code_country>
<country_id>605</country_id>
<country>Isle of Man</country>
<country_code>IMN</country_code>
</code_country>
<code_country>
<country_id>606</country_id>
<country>India</country>
<country_code>IND</country_code>
</code_country>
<code_country>
<country_id>607</country_id>
<country>British Indian Ocean Territory</country>
<country_code>IOT</country_code>
</code_country>
<code_country>
<country_id>609</country_id>
<country>Iran, Islamic Republic of</country>
<country_code>IRN</country_code>
</code_country>
<code_country>
<country_id>609</country_id>
<country>Iran</country>
<country_code>IRN</country_code>
</code_country>
<code_country>
<country_id>610</country_id>
<country>Iraq</country>
<country_code>IRQ</country_code>
</code_country>
<code_country>
<country_id>611</country_id>
<country>Iceland</country>
<country_code>ISL</country_code>
</code_country>
<code_country>
<country_id>612</country_id>
<country>Israel</country>
<country_code>ISR</country_code>
</code_country>
<code_country>
<country_id>614</country_id>
<country>Jamaica</country>
<country_code>JAM</country_code>
</code_country>
<code_country>
<country_id>615</country_id>
<country>Jersey</country>
<country_code>JEY</country_code>
</code_country>
<code_country>
<country_id>616</country_id>
<country>Jordan</country>
<country_code>JOR</country_code>
</code_country>
<code_country>
<country_id>617</country_id>
<country>Japan</country>
<country_code>JPN</country_code>
</code_country>
<code_country>
<country_id>618</country_id>
<country>Kazakhstan</country>
<country_code>KAZ</country_code>
</code_country>
<code_country>
<country_id>619</country_id>
<country>Kenya</country>
<country_code>KEN</country_code>
</code_country>
<code_country>
<country_id>620</country_id>
<country>Kyrgyzstan</country>
<country_code>KGZ</country_code>
</code_country>
<code_country>
<country_id>621</country_id>
<country>Cambodia</country>
<country_code>KHM</country_code>
</code_country>
<code_country>
<country_id>622</country_id>
<country>Kiribati</country>
<country_code>KIR</country_code>
</code_country>
<code_country>
<country_id>623</country_id>
<country>Saint Kitts and Nevis</country>
<country_code>KNA</country_code>
</code_country>
<code_country>
<country_id>624</country_id>
<country>Korea, Republic of</country>
<country_code>KOR</country_code>
</code_country>
<code_country>
<country_id>624</country_id>
<country>Korea</country>
<country_code>KOR</country_code>
</code_country>
<code_country>
<country_id>625</country_id>
<country>Kuwait</country>
<country_code>KWT</country_code>
</code_country>
<code_country>
<country_id>626</country_id>
<country>Lao People's Democratic Republic</country>
<country_code>LAO</country_code>
</code_country>
<code_country>
<country_id>627</country_id>
<country>Lebanon</country>
<country_code>LBN</country_code>
</code_country>
<code_country>
<country_id>628</country_id>
<country>Liberia</country>
<country_code>LBR</country_code>
</code_country>
<code_country>
<country_id>629</country_id>
<country>Libya</country>
<country_code>LBY</country_code>
</code_country>
<code_country>
<country_id>630</country_id>
<country>Saint Lucia</country>
<country_code>LCA</country_code>
</code_country>
<code_country>
<country_id>631</country_id>
<country>Liechtenstein</country>
<country_code>LIE</country_code>
</code_country>
<code_country>
<country_id>632</country_id>
<country>Sri Lanka</country>
<country_code>LKA</country_code>
</code_country>
<code_country>
<country_id>633</country_id>
<country>Lesotho</country>
<country_code>LSO</country_code>
</code_country>
<code_country>
<country_id>634</country_id>
<country>Lithuania</country>
<country_code>LTU</country_code>
</code_country>
<code_country>
<country_id>635</country_id>
<country>Luxembourg</country>
<country_code>LUX</country_code>
</code_country>
<code_country>
<country_id>636</country_id>
<country>Latvia</country>
<country_code>LVA</country_code>
</code_country>
<code_country>
<country_id>637</country_id>
<country>Macao</country>
<country_code>MAC</country_code>
</code_country>
<code_country>
<country_id>638</country_id>
<country>Saint Martin (French part)</country>
<country_code>MAF</country_code>
</code_country>
<code_country>
<country_id>639</country_id>
<country>Morocco</country>
<country_code>MAR</country_code>
</code_country>
<code_country>
<country_id>640</country_id>
<country>Monaco</country>
<country_code>MCO</country_code>
</code_country>
<code_country>
<country_id>641</country_id>
<country>Moldova, Republic of</country>
<country_code>MDA</country_code>
</code_country>
<code_country>
<country_id>642</country_id>
<country>Madagascar</country>
<country_code>MDG</country_code>
</code_country>
<code_country>
<country_id>643</country_id>
<country>Maldives</country>
<country_code>MDV</country_code>
</code_country>
<code_country>
<country_id>645</country_id>
<country>Marshall Islands</country>
<country_code>MHL</country_code>
</code_country>
<code_country>
<country_id>646</country_id>
<country>Macedonia, the former Yugoslav Republic of</country>
<country_code>MKD</country_code>
</code_country>
<code_country>
<country_id>646</country_id>
<country>Macedonia</country>
<country_code>MKD</country_code>
</code_country>
<code_country>
<country_id>647</country_id>
<country>Mali</country>
<country_code>MLI</country_code>
</code_country>
<code_country>
<country_id>648</country_id>
<country>Malta</country>
<country_code>MLT</country_code>
</code_country>
<code_country>
<country_id>649</country_id>
<country>Myanmar</country>
<country_code>MMR</country_code>
</code_country>
<code_country>
<country_id>650</country_id>
<country>Montenegro</country>
<country_code>MNE</country_code>
</code_country>
<code_country>
<country_id>651</country_id>
<country>Mongolia</country>
<country_code>MNG</country_code>
</code_country>
<code_country>
<country_id>652</country_id>
<country>Northern Mariana Islands</country>
<country_code>MNP</country_code>
</code_country>
<code_country>
<country_id>653</country_id>
<country>Mozambique</country>
<country_code>MOZ</country_code>
</code_country>
<code_country>
<country_id>654</country_id>
<country>Mauritania</country>
<country_code>MRT</country_code>
</code_country>
<code_country>
<country_id>655</country_id>
<country>Montserrat</country>
<country_code>MSR</country_code>
</code_country>
<code_country>
<country_id>656</country_id>
<country>Martinique</country>
<country_code>MTQ</country_code>
</code_country>
<code_country>
<country_id>657</country_id>
<country>Mauritius</country>
<country_code>MUS</country_code>
</code_country>
<code_country>
<country_id>658</country_id>
<country>Malawi</country>
<country_code>MWI</country_code>
</code_country>
<code_country>
<country_id>659</country_id>
<country>Malaysia</country>
<country_code>MYS</country_code>
</code_country>
<code_country>
<country_id>660</country_id>
<country>Mayotte</country>
<country_code>MYT</country_code>
</code_country>
<code_country>
<country_id>661</country_id>
<country>Namibia</country>
<country_code>NAM</country_code>
</code_country>
<code_country>
<country_id>662</country_id>
<country>New Caledonia</country>
<country_code>NCL</country_code>
</code_country>
<code_country>
<country_id>663</country_id>
<country>Niger</country>
<country_code>NER</country_code>
</code_country>
<code_country>
<country_id>664</country_id>
<country>Norfolk Island</country>
<country_code>NFK</country_code>
</code_country>
<code_country>
<country_id>665</country_id>
<country>Nigeria</country>
<country_code>NGA</country_code>
</code_country>
<code_country>
<country_id>666</country_id>
<country>Nicaragua</country>
<country_code>NIC</country_code>
</code_country>
<code_country>
<country_id>667</country_id>
<country>Niue</country>
<country_code>NIU</country_code>
</code_country>
<code_country>
<country_id>668</country_id>
<country>Netherlands</country>
<country_code>NLD</country_code>
</code_country>
<code_country>
<country_id>669</country_id>
<country>Norway</country>
<country_code>NOR</country_code>
</code_country>
<code_country>
<country_id>670</country_id>
<country>Nepal</country>
<country_code>NPL</country_code>
</code_country>
<code_country>
<country_id>671</country_id>
<country>Nauru</country>
<country_code>NRU</country_code>
</code_country>
<code_country>
<country_id>672</country_id>
<country>New Zealand</country>
<country_code>NZL</country_code>
</code_country>
<code_country>
<country_id>673</country_id>
<country>Oman</country>
<country_code>OMN</country_code>
</code_country>
<code_country>
<country_id>674</country_id>
<country>Pakistan</country>
<country_code>PAK</country_code>
</code_country>
<code_country>
<country_id>675</country_id>
<country>Panama</country>
<country_code>PAN</country_code>
</code_country>
<code_country>
<country_id>676</country_id>
<country>Pitcairn</country>
<country_code>PCN</country_code>
</code_country>
<code_country>
<country_id>677</country_id>
<country>Peru</country>
<country_code>PER</country_code>
</code_country>
<code_country>
<country_id>678</country_id>
<country>Philippines</country>
<country_code>PHL</country_code>
</code_country>
<code_country>
<country_id>679</country_id>
<country>Palau</country>
<country_code>PLW</country_code>
</code_country>
<code_country>
<country_id>680</country_id>
<country>Papua New Guinea</country>
<country_code>PNG</country_code>
</code_country>
<code_country>
<country_id>681</country_id>
<country>Poland</country>
<country_code>POL</country_code>
</code_country>
<code_country>
<country_id>682</country_id>
<country>Puerto Rico</country>
<country_code>PRI</country_code>
</code_country>
<code_country>
<country_id>683</country_id>
<country>Korea, Democratic People's Republic of</country>
<country_code>PRK</country_code>
</code_country>
<code_country>
<country_id>684</country_id>
<country>Portugal</country>
<country_code>PRT</country_code>
</code_country>
<code_country>
<country_id>685</country_id>
<country>Paraguay</country>
<country_code>PRY</country_code>
</code_country>
<code_country>
<country_id>686</country_id>
<country>Palestinian Territory, Occupied</country>
<country_code>PSE</country_code>
</code_country>
<code_country>
<country_id>687</country_id>
<country>French Polynesia</country>
<country_code>PYF</country_code>
</code_country>
<code_country>
<country_id>688</country_id>
<country>Qatar</country>
<country_code>QAT</country_code>
</code_country>
<code_country>
<country_id>689</country_id>
<country>Réunion</country>
<country_code>REU</country_code>
</code_country>
<code_country>
<country_id>690</country_id>
<country>Romania</country>
<country_code>ROU</country_code>
</code_country>
<code_country>
<country_id>691</country_id>
<country>Russia</country>
<country_code>RUS</country_code>
</code_country>
<code_country>
<country_id>692</country_id>
<country>Rwanda</country>
<country_code>RWA</country_code>
</code_country>
<code_country>
<country_id>693</country_id>
<country>Saudi Arabia</country>
<country_code>SAU</country_code>
</code_country>
<code_country>
<country_id>694</country_id>
<country>Sudan</country>
<country_code>SDN</country_code>
</code_country>
<code_country>
<country_id>695</country_id>
<country>Senegal</country>
<country_code>SEN</country_code>
</code_country>
<code_country>
<country_id>696</country_id>
<country>Singapore</country>
<country_code>SGP</country_code>
</code_country>
<code_country>
<country_id>697</country_id>
<country>South Georgia and the South Sandwich Islands</country>
<country_code>SGS</country_code>
</code_country>
<code_country>
<country_id>698</country_id>
<country>Saint Helena</country>
<country_code>SHN</country_code>
</code_country>
<code_country>
<country_id>699</country_id>
<country>Svalbard and Jan Mayen</country>
<country_code>SJM</country_code>
</code_country>
<code_country>
<country_id>700</country_id>
<country>Solomon Islands</country>
<country_code>SLB</country_code>
</code_country>
<code_country>
<country_id>701</country_id>
<country>Sierra Leone</country>
<country_code>SLE</country_code>
</code_country>
<code_country>
<country_id>702</country_id>
<country>El Salvador</country>
<country_code>SLV</country_code>
</code_country>
<code_country>
<country_id>703</country_id>
<country>San Marino</country>
<country_code>SMR</country_code>
</code_country>
<code_country>
<country_id>704</country_id>
<country>Somalia</country>
<country_code>SOM</country_code>
</code_country>
<code_country>
<country_id>705</country_id>
<country>Saint Pierre and Miquelon</country>
<country_code>SPM</country_code>
</code_country>
<code_country>
<country_id>706</country_id>
<country>Serbia</country>
<country_code>SRB</country_code>
</code_country>
<code_country>
<country_id>707</country_id>
<country>South Sudan</country>
<country_code>SSD</country_code>
</code_country>
<code_country>
<country_id>708</country_id>
<country>Sao Tome and Principe</country>
<country_code>STP</country_code>
</code_country>
<code_country>
<country_id>709</country_id>
<country>Suriname</country>
<country_code>SUR</country_code>
</code_country>
<code_country>
<country_id>710</country_id>
<country>Slovakia</country>
<country_code>SVK</country_code>
</code_country>
<code_country>
<country_id>711</country_id>
<country>Slovenia</country>
<country_code>SVN</country_code>
</code_country>
<code_country>
<country_id>712</country_id>
<country>Sweden</country>
<country_code>SWE</country_code>
</code_country>
<code_country>
<country_id>713</country_id>
<country>Swaziland</country>
<country_code>SWZ</country_code>
</code_country>
<code_country>
<country_id>714</country_id>
<country>Sint Maarten (Dutch part)</country>
<country_code>SXM</country_code>
</code_country>
<code_country>
<country_id>715</country_id>
<country>Seychelles</country>
<country_code>SYC</country_code>
</code_country>
<code_country>
<country_id>716</country_id>
<country>Syrian Arab Republic</country>
<country_code>SYR</country_code>
</code_country>
<code_country>
<country_id>717</country_id>
<country>Turks and Caicos Islands</country>
<country_code>TCA</country_code>
</code_country>
<code_country>
<country_id>718</country_id>
<country>Chad</country>
<country_code>TCD</country_code>
</code_country>
<code_country>
<country_id>719</country_id>
<country>Togo</country>
<country_code>TGO</country_code>
</code_country>
<code_country>
<country_id>720</country_id>
<country>Thailand</country>
<country_code>THA</country_code>
</code_country>
<code_country>
<country_id>721</country_id>
<country>Tajikistan</country>
<country_code>TJK</country_code>
</code_country>
<code_country>
<country_id>722</country_id>
<country>Tokelau</country>
<country_code>TKL</country_code>
</code_country>
<code_country>
<country_id>723</country_id>
<country>Turkmenistan</country>
<country_code>TKM</country_code>
</code_country>
<code_country>
<country_id>724</country_id>
<country>Timor-Leste</country>
<country_code>TLS</country_code>
</code_country>
<code_country>
<country_id>725</country_id>
<country>Tonga</country>
<country_code>TON</country_code>
</code_country>
<code_country>
<country_id>726</country_id>
<country>Trinidad and Tobago</country>
<country_code>TTO</country_code>
</code_country>
<code_country>
<country_id>727</country_id>
<country>Tunisia</country>
<country_code>TUN</country_code>
</code_country>
<code_country>
<country_id>728</country_id>
<country>Turkey</country>
<country_code>TUR</country_code>
</code_country>
<code_country>
<country_id>729</country_id>
<country>Tuvalu</country>
<country_code>TUV</country_code>
</code_country>
<code_country>
<country_id>730</country_id>
<country>Taiwan</country>
<country_code>TWN</country_code>
</code_country>
<code_country>
<country_id>731</country_id>
<country>Tanzania, United Republic of</country>
<country_code>TZA</country_code>
</code_country>
<code_country>
<country_id>732</country_id>
<country>Uganda</country>
<country_code>UGA</country_code>
</code_country>
<code_country>
<country_id>733</country_id>
<country>Ukraine</country>
<country_code>UKR</country_code>
</code_country>
<code_country>
<country_id>734</country_id>
<country>United States Minor Outlying Islands</country>
<country_code>UMI</country_code>
</code_country>
<code_country>
<country_id>735</country_id>
<country>Uruguay</country>
<country_code>URY</country_code>
</code_country>
<code_country>
<country_id>737</country_id>
<country>Uzbekistan</country>
<country_code>UZB</country_code>
</code_country>
<code_country>
<country_id>738</country_id>
<country>Holy See (Vatican City State)</country>
<country_code>VAT</country_code>
</code_country>
<code_country>
<country_id>739</country_id>
<country>Saint Vincent and the Grenadines</country>
<country_code>VCT</country_code>
</code_country>
<code_country>
<country_id>740</country_id>
<country>Venezuela, Bolivarian Republic of</country>
<country_code>VEN</country_code>
</code_country>
<code_country>
<country_id>741</country_id>
<country>Virgin Islands, British</country>
<country_code>VGB</country_code>
</code_country>
<code_country>
<country_id>742</country_id>
<country>Virgin Islands, U.S.</country>
<country_code>VIR</country_code>
</code_country>
<code_country>
<country_id>743</country_id>
<country>Viet Nam</country>
<country_code>VNM</country_code>
</code_country>
<code_country>
<country_id>744</country_id>
<country>Vanuatu</country>
<country_code>VUT</country_code>
</code_country>
<code_country>
<country_id>745</country_id>
<country>Wallis and Futuna</country>
<country_code>WLF</country_code>
</code_country>
<code_country>
<country_id>746</country_id>
<country>Samoa</country>
<country_code>WSM</country_code>
</code_country>
<code_country>
<country_id>747</country_id>
<country>Yemen</country>
<country_code>YEM</country_code>
</code_country>
<code_country>
<country_id>748</country_id>
<country>South Africa</country>
<country_code>ZAF</country_code>
</code_country>
<code_country>
<country_id>749</country_id>
<country>Zambia</country>
<country_code>ZMB</country_code>
</code_country>
<code_country>
<country_id>750</country_id>
<country>Zimbabwe</country>
<country_code>ZWE</country_code>
</code_country>
</countries>
class JtXmlParser
{
const XML_PATH ='datasource.xml';
const START_NODE = 'ROW'; // the element identifying a new record
public $file,
$rows = array(),
$record = array(),
$fdata = '',
$interactive;
//Constuctor
public function __construct($path = null, $startnode = null){
$this->file = $path;
$this->startnode = $startnode;
if(!isset($this->file)){
$this->file = self::XML_PATH;
}
if(!isset($this->startnode)){
$this->startnode = self::START_NODE;
}
$this->doParse();
}
//Factory create method
public static function get($path=null, $startnode=null){
return new self($path, $startnode);
}
//Prompt
private function prompt(){
echo sprintf('Found %s rows in XML source files%sContinue or quit (c/q) ?', count($this->rows), PHP_EOL);
}
//Finish up
public function finishUp(){
if($this->interactive){
$this->prompt();
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
$line = trim($line);
switch($line){
case 'C':
case 'c':
echo sprintf('Continuing...%s', PHP_EOL);
print_r($this->rows);
break;
default:
echo sprintf('Exiting...%s', PHP_EOL);
exit(0);
}
} else {
return $this->rows;
}
}
//Do Parse
public function doParse()
{
$this->record = array();
$this->fdata = '';
$this->inrecord = false;
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, array($this, "startElement"), array($this, "endElement"));
xml_set_character_data_handler($xml_parser, array($this, "characterData") );
if (!($fp = fopen($this->file, 'r'))) {
die('Could not open XML input');
}
while ($data = fread($fp, 4096)) {
//xml_parse will feed the current element to startElement() and endElement()
if (!xml_parse($xml_parser, $data, feof($fp)))
{
die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
}
/**
* Start Element
*/
public function startElement($parser, $name, $attrs)
{
// Start of new record?
if( $name == $this->startnode ) {
$this->inrecord = true;
$this->record = array();
}
$this->fdata = '';
}
/**
* End Element
*/
public function endElement($parser, $name)
{
//End of record
if( $name == $this->startnode ) {
$this->inrecord = false;
// Do end of record processing here
$this->rows[]= $this->record;
}
if( ! $this->inrecord ){
return;
}
// do something with the fdata element
if( ! isset($this->record[$name])) {
$this->record[$name] = $this->fdata;
} elseif( is_array($this->record[$name])) {
$this->record[$name][] = $this->fdata;
} else {
$temp = $this->record[$name];
$this->record[$name] = array($temp);
$this->record[$name][] = $this->fdata;
}
// clear it
$this->fdata = '';
}
public function setInteractive(){
$this->interactive = true;
return $this;
}
/**
* Character Data
*/
public function characterData($parser, $data)
{
$this->fdata .= $data;
}
}
<?php
//Populate $rows with multidimensional array of the parsed data
$rows = JtXmlParser::get('datasource.xml', 'CODE_COUNTRY')->finishUp();
//Render a nice HTML table
//Optionally, you can also run this as a command-line script which print_r's to std out.
//JtXmlParser::get('foodata.xml', 'CODE_COUNTRY')->setInteractive(true)->finishUp();
?>
<!DOCTYPE html>
<head>
<title>XML Parsing Tutorial</title>
<style type="text/css">body{ font-size:10px; line-height:12px; font-family:Verdana, sans-serif; }</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="http://cdn.datatables.net/1.10.3/js/jquery.dataTables.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('#example').DataTable();
} );
</script>
<link rel="stylesheet" href="http://cdn.datatables.net/1.10.3/css/jquery.dataTables.css">
</head>
<body>
<h1>My Cool Data</h1>
<h3><?php echo date('m/d/Y'); ?></h3>
<div>
<table id="example" class="display">
<thead>
<tr>
<?php foreach($rows[0] as $k=>$v): ?>
<td><?php echo $k; ?></td>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach($rows as $row): ?>
<tr>
<?php foreach($row as $k=>$v): ?>
<td><?php echo $v; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody></tfoot>
</tfoot>
</table>
</div>
</body>
</html>
<?php exit(0); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment