Page 1 of 1

XML Parsing code help please!

Posted: Sat Aug 18, 2007 7:23 am
by Jester
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi guys,

I'm trying to write a PHP script which will parse the following XML:

[syntax="xml"]<MyBook>
     <ArticleName>
            <Heading>Article 1</Heading>
            <Quote1>Some text</Quote1>
            <Quote2>Some other text</Quote2>
            <Quote3>Even more text</Quote3>
            <Article>Blah blah blah blah blah blah blah blah blah</Article>
            <Comment1>A comment goes here</Comment>
            <Comment2>Another comment here</Comment2>
      </ArticleName>
      <ArticleName>
            <Heading>Article2</Heading>
blah blah blah to Article 80 or so
</MyBook>
I know just enough PHP to be able to format the output as I would like, but I have never used XML before, and I can't seem to get any examples I have found so far to work.

My goal is to be able to call http://www.mysite.com/articleget.php?article=MyArticle1 or similar and have it return the correct article in the XML database that matches the Heading tag.

Can anyone please help? I tried reading all the documentation and I'm just getting more and more confused!


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sat Aug 18, 2007 7:42 am
by timvw
Exaclty which part in the documentation made you feel confused? Show us the code you've tried already (and what your reasoning is behind that code).. this way we can really help you...

Posted: Sat Aug 18, 2007 8:49 am
by thewebdrivers
Check this out. probably it will help. http://www.wirelessdevnet.com/channels/ ... t_php.html

Re: XML Parsing code help please!

Posted: Sat Aug 18, 2007 9:32 am
by superdezign
Jester wrote:but I have never used XML before, and I can't seem to get any examples I have found so far to work.
XML is just HTML without predefined tags (or, more accurately, HTML is XML with predefined tags). There are standards for RSS, but XML in itself is just tags. Not much you need to understand.

Posted: Sat Aug 18, 2007 11:13 am
by Chris Corbyn
Agreed, pure XML is just a siple document format. But learning everything that goes with it (DTD, XPath, XSL, DOM, SAX) is where experience comes in. I love XML for storing configuration information... it's easy to write, easy to relate one element of data to another, easy to read and easy to parse.

~Jester, look into XPath:

http://www.w3schools.com/xpath/

Then have a play with the DOM XML functions:

http://uk2.php.net/manual/en/ref.domxml.php

Maybe: http://uk2.php.net/manual/en/function.xpath-eval.php

Read the user comments on that last page I linked to.

Posted: Mon Aug 20, 2007 12:05 pm
by Jester
Thanks for your help guys! I'll check into this when I have a bit more time.

Mods - sorry about my code posting! It won't happen again :)