SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    *********! *********!!! jackli's Avatar
    Join Date
    Sep 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    xml example on php.net

    i'm trying to understand example1 on http://us2.php.net/xml

    It seems like if you take out:
    PHP Code:
       for ($i 0$i $depth[$parser]; $i++) {
           echo 
    "  ";
       } 
    and make endElement($parser,$name) a blank function (i.e., take out its content), and remove all references to $depth, you can still get an array out of the contents of the XML file.

    so:

    1) i'm not sure what the $depth variable does or what it's used for.
    2) i'm also not sure why they put in a for loop that only echo's " "...
    3) how is $depth[$parser] being incremented?
    4) if i were to take out the for loop of spaces i quoted above, and eliminate the contents of endElement and all references to $depth, would i be missing any of the XML data?

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,156
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)

    spaces and endElement

    1: The $depth variable keeps track of how deep the child nodes are. eg. below, "title" and "content" would each be 1 deep, "subtitle" is 2 deep.
    2: The spaces indent child nodes for better XML readability. eg.
    HTML Code:
    <item>
        <title>one</title>
            <subtitle>a</subtitle>
        <content>blah, blah, blah</content>
    </item>
    instead of
    HTML Code:
    <item>
    <title>one</title>
    <subtitle>a</subtitle>
    <content>blah, blah, blah</content>
    </item>
    3: $depth[$parser] is incremented in the startElement function and decremented in the endElement function. When the parser comes across a tag (<start> or </end>) the appropriate function changes the variable's value.
    4: You could safely take out the spaces loop if you didn't mind losing the indentation (IMHO a few extra spaces for much easier readability is worth it, especially with more complex files). I don't know what would happen if you removed all the deptth stuff, but I have a feeling it wouldn't be good. I guess you could try it and see.


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
  •