SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru Majglow's Avatar
    Join Date
    Aug 1999
    Location
    B-Town
    Posts
    645
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XML parsing into a DOM + efficiency questions

    hey guys,

    Alright, what I would like to do is parse an XML file, and place it all into a tree structure (DOM I think it's called).

    Right, I know that there are PHP functions to do this, however they are experimental. Also, I bet not a lot of hosts support them. So, I would like to do it myself. However, there are will be 2 differnent kinds of tags, what I call 'structure' tags, and 'style' tags. Lemme give you an example

    Code:
    <article>
      <title>Title comes here</title>
      <page>
         <section>
           <title>Title</title>
           <body>Body comes here <b>bold text</b></body>
         </section>
         <section>
           <title>Title comes here</title>
           <body><p>Here is body 2</p><p>new paragraph</p></body>
         </section>
      </page>
      <page>
          <section>
             <title>Title 3</title>
             <body>This section is on a differnent page</body>
          </section>
      </page>
    </article>
    Right, I'm sure you get the general idea, so the only 'style' tags in there would be '<p>' and '<b>', the rest are structure tags.

    Now, what would be the difference? Well, when the XML document is parsed, the style tags would just be replaced with HTML in order to produce the right effect and would be included as text in the "DOM". Now, let me further the idea of the DOM (what I view it as at least)

    So, in the presented example, it would be something like:

    $dom->article->page[0]->section[0]->title->content = "Title"

    $dom->article->page[0]->section[0]->body->content = "Body comes here <b>bold text</b>"
    (Note, the '<b>...</b>' is simply html, not xml anymore).

    stuff like that..

    So... what would be the best way to do that? Using the XML functions PHP provides, I can't quite figure out how to convert some tags to html and others as parts of the DOM.

    Also, how efficient would this be? Would this be good enough to perform every time an article is loaded from a CMS? Or would there be a better way to do it?

    The reason I wanna do it this way, is so I can have an HTML template, and just have $dom->article->title->content wherever I wanna put the title, like that it's easy to make new templates, etc..

    Any thoughts?
    -cARL
    Ohai!

  2. #2
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Apparently PHP's OOP is quite inefficient. For normal use of objects (for example a database abstraction layer) performance is not an issue, but the system you are suggesting could have potentially hundreds of objects nested within each other. I've not seen any benchmarks about this but it is something you should definitely bare in mind.

    There is already a PHP-only (i.e not relying on PHP's built in XML handling functions) solution for XML which you may want to have a look at:

    http://sourceforge.net/projects/phpxpath/

  3. #3
    SitePoint Guru Majglow's Avatar
    Join Date
    Aug 1999
    Location
    B-Town
    Posts
    645
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm...

    I don't really like any of the already available stuff.

    Any ideas?

    -cARL
    Ohai!

  4. #4
    SitePoint Member
    Join Date
    Mar 2002
    Location
    China
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Best solution is XSL/XSLT

    I am also considering how to best use xml in my site.
    After long time reading materials from the web, i think the best solution is XML+XSL+XSLT.
    For your example, i think you can write a xsl file, in which all style tags are not processed, say for body:

    <xsl:template match="/page">
    <html><head>
    <title><xsl:value-of select="title"/></title>
    </head>
    <body>
    <xsl:value-of select="body"/>
    </body>
    </xsl:template>

    and you can then use xslt extension of PHP to process it.
    For more info, please refer to php manual.
    after 4.10, the xslt in php is a generic solution other than sabletron in older versions, so the codes are different.
    xslt_process() page gives some good examples.

  5. #5
    SitePoint Guru Majglow's Avatar
    Join Date
    Aug 1999
    Location
    B-Town
    Posts
    645
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I looked at XSLT. It is a good system, only there are a few drawbacks. Those being that not many hosts support XSLT, there are a few, but not really enough. I want to make a script that is useable on a good majority of servers. Also, XSLT doesn't make it too easy to make a template that would be easy to make content spread on multiple pages. I mean, it wouldn't be easy for the average user, and I'm making a script that not the most tech savy user is going to be using.

    I decided to opt for the normal XML functions, and make my own parser and validator, that would only parse the xml when it is submitted to the CMS, then converted partially into html, and then stuck into the rest of the template when the page is loaded.

    What do you guys think of that solution?

    -cARL
    Ohai!

  6. #6
    SitePoint Zealot marcoBR's Avatar
    Join Date
    Jun 2002
    Location
    Brazil
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    India
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XML, XSL & XSLT

    For tutorials check out...
    http://www.w3schools.com/xml/default.asp

    check out this thread for more on XML, XSL/XSLT
    http://www.sitepointforums.com/showt...8&goto=newpost

    rgds
    Sukesh
    thnX n Rgds
    Sukesh Ashok Kumar
    Brain Behind - http://www.V4Cnet.com
    Microsoft MVP in .NET


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •