Jump to content

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


mykel241

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>

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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>';


?>

Link to comment
Share on other sites

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.