Jump to content

Help with parsing XML file part II - Need to place value of names into an array


Recommended Posts

If someone can help me to parse this file and put the results into an array it would be great. I need the value between the name tags put into an array so I can print it out via a foreach loop. I am looking for the simplest option available. Thank you.

 

XML sample:

 

<?xml version="1.0" encoding="UTF-8"?>

 

<webservices ver="1.0">

  <status code="1.0"/>

  <markets>

    <market weight="10">

      <name>Boston</name>

    </market>

    <market weight="10">

      <name>Washington DC</name>

    </market>

http://www.phpfreaks.com/forums/index.php/topic,148829.0.html

 

Given the code in that post I would hope you can figure it out. The code is there just needs modifying. We are not here to code for you.

try

<?php
$file = '<?xml version="1.0" encoding="UTF-8"?>

<webservices ver="1.0">
  <status code="1.0"/>
  <markets>
    <market weight="10">
      <name>Boston</name>
    </market>
    <market weight="10">
      <name>Washington DC</name>
    </market>';
preg_match_all('/<name>(.+?)<\/name>/', $file, $m);
$names = $m[1];
print_r($names);
?>

or try simplexml

 

<?php
$str = '<webservices ver="1.0">
  <status code="1.0"/>
  <markets>
    <market weight="10">
      <name>Boston</name>
    </market>
    <market weight="10">
      <name>Washington DC</name>
    </market>
  </markets>
</webservices>';

$xml = simplexml_load_string($str);

foreach ($xml->xpath('/webservices/markets/market/name') as $name) echo $name.'<br>';


?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.