XML parsing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sandman

    XML parsing

    Hello

    I have been trying to wrap my head arouns this, but I can't seem to get it to
    work the way I want it to. Could someone helpful please tell me how I get this:

    <function>
    <name>array_pus h</name>
    <examples>
    <example>
    <name>A way to do it</name>
    <code>
    $array = array("foo", "bar")
    $array = array_push($arr ay, "goo")
    </code>
    </example>
    </examples>
    </function>

    To this:

    array (
    name -> array_push
    examples -> array(
    example -> array(
    name -> A way to do it
    code -> $array = array("foo", "bar")
    $array = array_push($arr ay, "goo")
    )
    )
    )

    My problems so far has been traversing the xml in a good way and:

    1. After parsing, I am left with the code stripped of variables
    '$array = array("foo", "bar")' becomes ' = array("foo", "bar")'

    2. Newlines in the code disappaers, how do I make them stay?

    Anyone feeling overly helpful and could give me an example to make the above
    xml into the above array.

    --
    Sandman[.net]
  • Sandman

    #2
    Re: XML parsing

    In article <mr-9EE649.19505212 092003@news.fu-berlin.de>,
    Sandman <mr@sandman.net > wrote:
    [color=blue]
    > Hello
    >
    > I have been trying to wrap my head arouns this, but I can't seem to get it to
    > work the way I want it to. Could someone helpful please tell me how I get
    > this:
    >
    > <function>
    > <name>array_pus h</name>
    > <examples>
    > <example>
    > <name>A way to do it</name>
    > <code>
    > $array = array("foo", "bar")
    > $array = array_push($arr ay, "goo")
    > </code>
    > </example>
    > </examples>
    > </function>
    >
    > To this:
    >
    > array (
    > name -> array_push
    > examples -> array(
    > example -> array(
    > name -> A way to do it
    > code -> $array = array("foo", "bar")
    > $array = array_push($arr ay, "goo")
    > )
    > )
    > )
    >
    > My problems so far has been traversing the xml in a good way and:
    >
    > 1. After parsing, I am left with the code stripped of variables
    > '$array = array("foo", "bar")' becomes ' = array("foo", "bar")'
    >
    > 2. Newlines in the code disappaers, how do I make them stay?
    >
    > Anyone feeling overly helpful and could give me an example to make the above
    > xml into the above array.[/color]


    Is there really no one who has a simple solution to the above?

    --
    Sandman[.net]

    Comment

    • Zurab Davitiani

      #3
      Re: XML parsing

      Sandman wrote on Monday 15 September 2003 00:38:
      [color=blue][color=green]
      >> <function>
      >> <name>array_pus h</name>
      >> <examples>
      >> <example>
      >> <name>A way to do it</name>
      >> <code>
      >> $array = array("foo", "bar")
      >> $array = array_push($arr ay, "goo")
      >> </code>
      >> </example>
      >> </examples>
      >> </function>
      >>
      >> To this:
      >>
      >> array (
      >> name -> array_push
      >> examples -> array(
      >> example -> array(
      >> name -> A way to do it
      >> code -> $array = array("foo", "bar")
      >> $array = array_push($arr ay, "goo")
      >> )
      >> )
      >> )
      >>
      >> My problems so far has been traversing the xml in a good way and:
      >>
      >> 1. After parsing, I am left with the code stripped of variables
      >> '$array = array("foo", "bar")' becomes ' = array("foo", "bar")'
      >>
      >> 2. Newlines in the code disappaers, how do I make them stay?
      >>
      >> Anyone feeling overly helpful and could give me an example to make the
      >> above xml into the above array.[/color]
      >
      >
      > Is there really no one who has a simple solution to the above?
      >[/color]

      I didn't get your first post, just your own follow-up. Here's one solution:

      1. Get ActiveLink PHP XML Package from:


      OR
      Download ActiveLink PHP XML Package for free. ActiveLink PHP XML Package provides means to parse, read, modify and output XML and XML documents without using any PHP XML libraries. Included classes are: XML, XMLDocument, XMLBranch, XMLLeaf, RSS, Tag, Tree, Branch, Leaf, File, Socket, HTTPClient.


      2. It helps to read docs at this point.

      3. Parse your XML string into an XML object like:
      $functionXML = new XML($functionXM LString);
      OR, if you are reading from a file, you can use XMLDocument object for that
      too.

      4. Use something like following function to get your array (warning -
      untested code):

      function convertXMLToArr ay($xml) {
      $arrNew = array();
      if(get_class($x ml) != "xml" && get_class($xml) != "xmlbranch" )
      return(false);
      else {
      if($xml->hasBranch()) {
      $branches = $xml->getBranches( );
      foreach($branch es as $branch) {
      $tagName = $branch->getTagName() ;
      if($branch->hasBranch())
      $arrNew[$tagName] = convertXMLToArr ay($branch);
      else
      $arrNew[$tagName] = $branch->getTagContent( );
      }
      }
      else
      $arrNew[] = $xml->getTagContent( );
      }
      return($arrNew) ;
      }

      Note: If you specifically need an array for some reason, use the above; if
      you don't need an array, but need a way to store, access, and modify XML,
      then simply use XML object methods to do so, i.e. you won't need to mess
      with arrays and the above function at all.

      Good luck!

      Disclaimer: I am the author of ActiveLink PHP XML Package; the package is
      available under LGPL.

      --
      Business Web Solutions
      ActiveLink, LLC

      Comment

      • Dalibor Karlovic

        #4
        Re: XML parsing

        Zurab Davitiani wrote:
        [color=blue]
        > Note: If you specifically need an array for some reason, use the above; if
        > you don't need an array, but need a way to store, access, and modify XML,
        > then simply use XML object methods to do so, i.e. you won't need to mess
        > with arrays and the above function at all.[/color]

        What I really don't get is what he's going to do with the data. If he's
        gonna print it, then XSL is the way to go, IMHO.
        [color=blue]
        > Disclaimer: I am the author of ActiveLink PHP XML Package; the package is
        > available under LGPL.[/color]

        No harm in little self promotion. :) Nice job on the package.

        --
        Dado

        Somewhere, just out of sight, the unicorns are gathering.

        Comment

        • Sandman

          #5
          Re: XML parsing

          In article <9edg31-qv1.ln1@amelie. ofr.hr>,
          Dalibor Karlovic <dado@krizevci. info> wrote:
          [color=blue][color=green]
          > > Note: If you specifically need an array for some reason, use the above; if
          > > you don't need an array, but need a way to store, access, and modify XML,
          > > then simply use XML object methods to do so, i.e. you won't need to mess
          > > with arrays and the above function at all.[/color]
          >
          > What I really don't get is what he's going to do with the data. If he's
          > gonna print it, then XSL is the way to go, IMHO.[/color]

          Ok, please elaborate - any suggestions are welcome!

          --
          Sandman[.net]

          Comment

          • Dalibor Karlovic

            #6
            Re: XML parsing

            Sandman wrote:

            I appolagize for the length of the post.
            [color=blue][color=green]
            >> What I really don't get is what he's going to do with the data. If he's
            >> gonna print it, then XSL is the way to go, IMHO.[/color]
            >
            > Ok, please elaborate - any suggestions are welcome![/color]

            You do like this:

            <?php
            // XML + XSL
            $oXsl = xslt_create();

            if ($sResult = xslt_process($o Xsl, 'test.xml', 'test.xsl', NULL)){
            echo $sResult;
            } else {
            die (xslt_error($oX sl));
            }

            xslt_free($oXsl );
            ?>

            This will parse files test.xml and test.xsl which are located where the
            script is located. The content of the files:

            (test.xml)

            <functions>
            <function>
            <name>array_pus h</name>
            <examples>
            <example>
            <name>a way to do it</name>
            <code>
            <![CDATA[
            $myArray = array("foo", "bar");
            $myArray = array_push($myA rray, "goo");
            ]]>
            </code>
            </example>
            <example>
            <name>another way</name>
            <code>
            <![CDATA[
            $myArray = array("foo", "bar", "goo");
            $myArray = array_push($myA rray, "goo");
            ]]>
            </code>
            </example>
            </examples>
            </function>
            <function>
            <name>array_pop </name>
            <examples>
            <example>
            <name>you use it like this (code doesn't work :)</name>
            <code>
            <![CDATA[
            $myArray = array("foo", "bar");
            $myArray = array_pop($myAr ray, "goo");
            ]]>
            </code>
            </example>
            </examples>
            </function>
            </functions>

            test.xsl

            <xsl:styleshe et version="1.0"
            xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
            <xsl:template match="function ">
            <h1>
            <xsl:value-of select="name"/>
            </h1>
            <xsl:for-each select="example s">
            <xsl:for-each select="example ">
            <div>
            <xsl:value-of select="name"/>
            <pre>
            <code>
            <xsl:value-of select="code"/>
            </code>
            </pre>
            </div>
            </xsl:for-each>
            </xsl:for-each>
            </xsl:template>
            </xsl:stylesheet>

            Output I got when finished:

            <?xml version="1.0" encoding="UTF-8"?>
            <h1>array_pus h</h1><div>a way to do it<pre><code>

            $myArray = array("foo", "bar");
            $myArray = array_push($myA rray, "goo");

            </code></pre></div><div>anothe r way<pre><code>

            $myArray = array("foo", "bar", "goo");
            $myArray = array_push($myA rray, "goo");

            </code></pre></div>
            <h1>array_pop </h1><div>you use it like this (code doesn't work
            :)<pre><code>

            $myArray = array("foo", "bar");
            $myArray = array_pop($myAr ray, "goo");

            </code></pre></div>

            I am _really_ new to all this XSL business so this probably can be done much
            cleaner, but this works :)

            --
            Dado

            There is no time like the pleasant.

            Comment

            Working...