Reply to Re: weather file - accessing xml data

Your name:

Reply:


Posted by Steve on 09/24/07 16:15

"z1" <my@email123.net> wrote in message
news:46f73d2c$0$19652$4c368faf@roadrunner.com...
> hi -
>
> i am going to try to use regular expressions to get my values.
> i am frustrated that the formart of the file doesnt seem to be
> able to be parsed by simplexml.
>
> thanks anyways,

listen jim, don't try to access the arrays by index. just use a foreach.
however, if you just want to use regex for simplicity and you don't need to
know attributes and such, try these:

==========

hint: segment is a node

<?xml>
<root>
<records> would be a 'segment'...a 'single segment'
<record /> would be one of many 'segments', or 'multiple segments'
<record />
<record />
</records>
</root>

$records = getSingleSegment('records', $xml);
$records = getMultipleSegments('record', $records);
foreach ($records as $record)
{
// you get the idea
}

==========

function getMultipleSegments($segment, $xml)
{
$pattern = "/<(" . $segment . ")>(.*?)<\/\\1>/si";
preg_match_all($pattern, $xml, $matches);
foreach ($matches[2] as $content)
{
$output[] = $content;
}
return $output;
}

function getMultipleSegmentNames($xml)
{
$output = array();
$pattern = "/<([^>]+)>.*?<\/\\1>/si";
preg_match_all($pattern, $xml, $matches);
foreach ($matches[1] as $content)
{
$output[] = $content;
}
return $output;
}

function getSegmentValue($xml, $emptyDefault = '')
{
$pattern = "/<([^>]+)>(.*?)<\/\\1>/si";
preg_match($pattern, $xml, $match);
$value = $match[2];
if ($value == ''){ $value = $emptyDefault; }
return htmlDecode($value);
}

function getSingleSegment($segment, $xml)
{
$output = '';
$pattern = "/<(" . $segment . ")>(.*?)<\/\\1>/si";
preg_match_all($pattern, $xml, $matches);
foreach ($matches[0] as $content)
{
$output .= $content;
}
return $output;
}

function getSingleSegmentName($xml)
{
$output = '';
$pattern = "/<([^>]+)>.*?<\/\\1>/si";
preg_match($pattern, $xml, $match);
return $match[1];
}

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация