How to parse Xml?
am 06.08.2007 17:44:47 von php.developer2007
Hi i want to parse a xml document which contains attribute can anybody
help me in that.
Example
--------------------
I want to get all id's in this example with using regular
expressions..
Output
----------
1
2
3
Re: How to parse Xml?
am 06.08.2007 18:06:57 von musiccomposition
On Aug 6, 9:44 am, php.developer2...@gmail.com wrote:
> Hi i want to parse a xml document which contains attribute can anybody
> help me in that.
>
> Example
> --------------------
>
>
>
>
> I want to get all id's in this example with using regular
> expressions..
>
> Output
> ----------
> 1
> 2
> 3
First of all that's not valid XML. In order to parse it, it has to be
valid. This, however, is easily fixed in your case. Just put the whole
body in tags and add the XML declaration like this:
With PHP, you have many options to parse XML with. My favorite and
probably the most easy is SimpleXML. Look at http://www.php.net/simplexml.
Re: How to parse Xml?
am 06.08.2007 18:40:16 von php.developer2007
On Aug 6, 9:06 pm, Benjamin wrote:
> On Aug 6, 9:44 am, php.developer2...@gmail.com wrote:
>
>
>
>
>
> > Hi i want to parse a xml document which contains attribute can anybody
> > help me in that.
>
> > Example
> > --------------------
> >
> >
> >
>
> > I want to get all id's in this example with using regular
> > expressions..
>
> > Output
> > ----------
> > 1
> > 2
> > 3
>
> First of all that's not valid XML. In order to parse it, it has to be
> valid. This, however, is easily fixed in your case. Just put the whole
> body in tags and add the XML declaration like this:
>
>
>
>
>
>
>
> With PHP, you have many options to parse XML with. My favorite and
> probably the most easy is SimpleXML. Look athttp://www.php.net/simplexml.- Hide quoted text -
>
> - Show quoted text -
Yeah I know i m just giving an example.I want it to be done with
preg_match_all as i had done this before with it.help if any body
help.I m facing problem this time.
Re: How to parse Xml?
am 06.08.2007 20:58:12 von Evan Charlton
php.developer2007@gmail.com wrote:
> Yeah I know i m just giving an example.I want it to be done with
> preg_match_all as i had done this before with it.help if any body
> help.I m facing problem this time.
>
A very good site for regular expression help is
http://regular-expressions.info/ . It sounds as if you're looking for a
handout, and learning is better than copying. Post a sample that isn't
working for you, along with the output you're getting, and we can see
what's wrong with it.
- Evan Charlton
Re: How to parse Xml?
am 06.08.2007 21:00:45 von Erwin Moller
php.developer2007@gmail.com wrote:
> On Aug 6, 9:06 pm, Benjamin wrote:
>> On Aug 6, 9:44 am, php.developer2...@gmail.com wrote:
>>
>>
>>
>>
>>
>>> Hi i want to parse a xml document which contains attribute can anybody
>>> help me in that.
>>> Example
>>> --------------------
>>>
>>>
>>>
>>> I want to get all id's in this example with using regular
>>> expressions..
>>> Output
>>> ----------
>>> 1
>>> 2
>>> 3
>> First of all that's not valid XML. In order to parse it, it has to be
>> valid. This, however, is easily fixed in your case. Just put the whole
>> body in tags and add the XML declaration like this:
>>
>>
>>
>>
>>
>>
>>
>> With PHP, you have many options to parse XML with. My favorite and
>> probably the most easy is SimpleXML. Look athttp://www.php.net/simplexml.- Hide quoted text -
>>
>> - Show quoted text -
>
> Yeah I know i m just giving an example.I want it to be done with
> preg_match_all as i had done this before with it.help if any body
> help.I m facing problem this time.
>
Hi,
If you want to do it with regexpressions, you could try something like this.
Please note I do it in 2 steps, simply because I suck at regexp to do it
in 1 step.
Normally I do not respond to regexpression questions, because of the
abovementioned reason, but since you need it quickly, here it comes.
I tried with 'look behind' and 'look ahead' but of course that didn't
suceed in my case. :P
// get your data in a var. Just an example
$var='\n
parent-id="1" level="3">\n';
$expr = '/\ id="[0-9]+"/';
preg_match_all($expr,$var,$results);
echo "";
print_r($results);
echo "
";
// results now contain an array with [0] => id="031" , [1] =>
id="233", [2] => id="31"
$allresultsStr = implode("",$results[0]) ;
// get the numbers out
preg_match_all('/[0-9]+/',$allresultsStr,$results2);
echo "";
print_r($results2);
echo "
";
?>
Regards,
Erwin Moller
Re: How to parse Xml?
am 06.08.2007 22:15:26 von Toby A Inkster
Benjamin wrote:
> First of all that's not valid XML.
[...]
>
>
>
>
>
>
Nor is that.
--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 46 days, 23:54.]
Command Line Interfaces, Again
http://tobyinkster.co.uk/blog/2007/08/02/command-line-again/
Re: How to parse Xml?
am 06.08.2007 22:24:20 von Toby A Inkster
php.developer2007 wrote:
> Hi i want to parse a xml document which contains attribute can anybody
> help me in that.
http://www.php.net/dom
> I want to get all id's in this example with using regular
> expressions..
$xml = '
';
$document = new DomDocument;
$document->loadXML($xml);
$id_list = array();
foreach ($document->getElementsByTagName('category') as $category)
$id_list[] = $category->getAttribute('id');
print_r($id_list);
?>
--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 46 days, 23:56.]
Command Line Interfaces, Again
http://tobyinkster.co.uk/blog/2007/08/02/command-line-again/
Re: How to parse Xml?
am 07.08.2007 02:56:15 von musiccomposition
On Aug 6, 2:15 pm, Toby A Inkster
wrote:
> Benjamin wrote:
> > First of all that's not valid XML.
> [...]
> >
> >
> >
> >
> >
> >
>
> Nor is that.
well formed, I mean! :)
>
> --
> Toby A Inkster BSc (Hons) ARCS
> [Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
> [OS: Linux 2.6.12-12mdksmp, up 46 days, 23:54.]
>
> Command Line Interfaces, Again
> http://tobyinkster.co.uk/blog/2007/08/02/command-line-again/
Re: How to parse Xml?
am 07.08.2007 05:37:27 von Aerik
On Aug 6, 8:44 am, php.developer2...@gmail.com wrote:
> Hi i want to parse a xml document which contains attribute can anybody
> help me in that.
>
> Example
> --------------------
>
>
>
>
> I want to get all id's in this example with using regular
> expressions..
>
> Output
> ----------
> 1
> 2
> 3
Use this regex:
/]+/
Aerik
Re: How to parse Xml?
am 07.08.2007 07:36:49 von Toby A Inkster
Benjamin wrote:
> Toby A Inkster wrote:
>> Benjamin wrote:
>>
>>> First of all that's not valid XML.
>> [...]
>>>
>>>
>>>
>>>
>>>
>>>
>>
>> Nor is that.
>
> well formed, I mean! :)
It ain't well-formed either.
--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 47 days, 9:15.]
Command Line Interfaces, Again
http://tobyinkster.co.uk/blog/2007/08/02/command-line-again/
Re: How to parse Xml?
am 07.08.2007 12:35:57 von Erwin Moller
Ah, I learned something today. :-)
Here is one that works in 1 regex with lookbehind and ahead. :-)
$var='\n
parent-id="1" level="3">\n';
$expr = '/(?<=\sid=")\d+(?=")/';
preg_match_all($expr,$var,$results);
echo "";
print_r($results[0]);
echo "
";
?>
Re: How to parse Xml?
am 11.08.2007 04:20:01 von musiccomposition
On Aug 7, 12:36 am, Toby A Inkster
wrote:
> Benjamin wrote:
> > Toby A Inkster wrote:
> >> Benjamin wrote:
>
> >>> First of all that's not valid XML.
> >> [...]
> >>>
> >>>
> >>>
> >>>
> >>>
> >>>
>
> >> Nor is that.
>
> > well formed, I mean! :)
>
> It ain't well-formed either.
Ok, you win:
>
> --
> Toby A Inkster BSc (Hons) ARCS
> [Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
> [OS: Linux 2.6.12-12mdksmp, up 47 days, 9:15.]
>
> Command Line Interfaces, Again
> http://tobyinkster.co.uk/blog/2007/08/02/command-line-again/