Professional Web Applications Themes

XML Parsing - PHP Programming

Hello, I'm new to PHP but have been coding in Perl for over six years. I've had a steep learning curve in the last few days. My task has been to use fsockopen to pull in an XML stream, p that XML stream, and package up that data into a complex, but usable data structure. We've been able to accomplish this in Perl but now I'm attempting to port the Perl code to PHP. The Perl code was written by Randal Schwartz (for me) but he's not interested in helping me solve the PHP problems I'm running into. I'm hoping ...

  1. #1

    Default XML Parsing

    Hello,

    I'm new to PHP but have been coding in Perl for over six years. I've
    had a steep learning curve in the last few days. My task has been to
    use fsockopen to pull in an XML stream, p that XML stream, and
    package up that data into a complex, but usable data structure. We've
    been able to accomplish this in Perl but now I'm attempting to port the
    Perl code to PHP. The Perl code was written by Randal Schwartz (for me)
    but he's not interested in helping me solve the PHP problems I'm
    running into. I'm hoping to find someone on this list savvy in Perl
    and PHP, and understands complex data structures (in perl the hash
    refs/pointers go about four levels deep). I've included below the PHP
    code with three functions in question. Below each function is the Perl
    code I'm attempting to port.

    Here are some URL's that might help:

    [url]http://shop.sprocketdata.com/test.php[/url] - url for the code below (which
    works if you copy paste)
    ignore the feof error- i left out the fsockopen because I know that's
    not where the problem is nor does this error effect the output.

    [url]http://stage.holdit.com/cgi-bin/is/shipping.cgi[/url] - url for equivalent
    code in perl (ignore the drop-down)

    [url]http://stage.holdit.com/cgi-bin/is/fetch.cgi[/url] - the data structure I'm
    trying to achieve in PHP

    [url]http://stage.holdit.com/cgi-bin/is/is.cgi[/url] - the raw XML data structure
    - almost useless until pd and probably useless to look at :-).

    You'll also notice at the bottom of the code I've included how the PHP
    XML pd the data. It's very similar to the fetch.cgi - maybe it's
    different and thus causing the problem but I don't see it.

    <?php

    $state = array();
    global $state;

    // these two associatative arrays will hold the data structure (I hope
    :-)
    $quote = array();
    global $quote;
    $quotes = array();
    global $quotes;

    //XML start element
    function startElement(&$Pr, &$Elem, &$Attr) {
    global $state; //this works
    array_push ($state, $Elem); //this works
    $states = join (' ',$state); //can't interpolate the list so must
    join - works
    if ($states = "SHIPMENT PACKAGE") { //we make it this far
    echo "$states\n"; //for testing should on print once!
    // $package_id = $Attr->{id}; // how to write this in PHP?
    }
    //This conditional is never met - QUOTE gets popped off before it
    returns
    elseif ($states = "SHIPMENT PACKAGE QUOTE") { //why does perl see
    QUOTE?
    echo "$states\n"; // testing
    // %quote = ( package_id => $package_id, id => $attr->{id} ); //Perl
    // $quote = array ( package_id => $package_id, id => $attr->{id} );
    //PHP version of above line?
    }
    }

    /*Perl
    start_h => [ sub {
    my ($tagname, $attr) = _;
    push state, $tagname;
    if ("state" eq "shipment package") {
    print "state<br>";#testing
    $package_id = $attr->{id};
    }
    elsif ("state" eq "shipment package quote") {
    %quote = ( package_id => $package_id, id => $attr->{id} );
    print "state<br>";#testing
    }
    }, "tagname, attr"],
    */

    // XML data
    //This function might work but I don't know yet as QUOTE never stays in
    the $state stack
    function characterData(&$Pr, &$Line) {
    global $state;
    $states = join (' ',$state);
    if ($states = "SHIPPMENT ERROR") {
    $error = $Line;
    }
    elseif ($states = "SHIPMENT PACKAGE QUOTE BOXID") {
    $boxID = $Line;
    }
    elseif ($states = "SHIPMENT PACKAGE QUOTE CARRIER NAME") {
    $quote{carrier_name} = $Line; // Doubting this will work - need to
    fix this too
    }
    elseif ($states = "SHIPMENT PACKAGE QUOTE CARRIER CODE") {
    $quote{carrier_code} = $Line;
    }
    elseif ($states = "SHIPMENT PACKAGE QUOTE CLASS NAME") {
    $quote{class_name} = $Line;
    }
    elseif ($states = "SHIPMENT PACKAGE QUOTE CLASS CODE") {
    $quote{class_code} = $Line;
    }
    elseif ($states = "SHIPMENT PACKAGE QUOTE SERVICE NAME") {
    $quote{service_name} = $Line;
    }
    elseif ($states = "SHIPMENT PACKAGE QUOTE SERVICE CODE") {
    $quote{service_code} = $Line;
    }
    elseif ($states = "SHIPMENT PACKAGE QUOTE RATE AMOUNT") {
    $quote{amount} = $Line;
    }
    }

    /* Perl
    text_h => [ sub {
    my ($text) = _;
    if ("state" eq "shipment error") {
    $error = $text;
    }
    elsif ("state" eq "shipment package boxID") {
    $boxID = $text;
    }
    elsif ("state" eq "shipment package quote carrier name") {
    $quote{carrier_name} = $text;
    }
    elsif ("state" eq "shipment package quote carrier code") {
    $quote{carrier_code} = $text;
    }
    elsif ("state" eq "shipment package quote class name") {
    $quote{class_name} = $text;
    }
    elsif ("state" eq "shipment package quote class code") {
    $quote{class_code} = $text;
    }
    elsif ("state" eq "shipment package quote service name") {
    $quote{service_name} = $text;
    }
    elsif ("state" eq "shipment package quote service code") {
    $quote{service_code} = $text;
    }
    elsif ("state" eq "shipment package quote rate amount") {
    $quote{amount} = $text;
    }
    }, "dtext"],
    */


    function endElement(&$Pr, &$Elem) {
    global $state;
    $states = join (' ',$state);
    if ($states = "SHIPMENT PACKAGE QUOTE") {
    //echo "got QUOTE here $states\n";

    /*
    The following three lines need porting to PHP
    there is much perl magic here - I can explain if it doesn't
    make sense. The short of this is it's a rather complex structure
    Perhaps this is where things are breaking?
    */
    //$key = "quote{qw(carrier_code service_code)}";
    //delete quote{qw(id package_id)}; # artificial
    //$quotes{$boxID}{$key} = {%quote};
    }
    // obviously the part that pulls QUOTE off the $state stack
    // perhaps a conditional?
    array_pop($state);
    }

    /*Perl
    end_h => [ sub {
    my ($tagname) = _;
    if ("state" eq "shipment package quote") {
    my $key = "quote{qw(carrier_code service_code)}";
    delete quote{qw(id package_id)}; # artificial
    $quotes{$boxID}{$key} = {%quote};
    }
    pop state;
    }, "tagname"],
    */



    //This code should work just fine
    echo '<html><body><pre><form>';
    $xml_pr = xml_pr_create();
    xml_set_element_handler($xml_pr, "startElement", "endElement");
    xml_set_character_data_handler($xml_pr, "characterData");

    //The XML data as returned from the server in it's raw form
    $data = '<?xml version="1.0"?><shipment
    xsi:schemaLocation="http://www.intershipper.com/Interface/Intershipper/
    XML/v2.0/schemas response.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-
    instance"><version>2.0.0.0</version><shipmentID>1234</
    shipmentID><queryID>23456</queryID><deliveryType>COM</
    deliveryType><shipMethod>DRP</shipMethod><origination><name>John
    Smith</name><address1>465 Tyende
    Ovi</address1><address2></address2><address3></
    address3><city>Flagstaff</city><state>AZ</state><postal>86001</
    postal><country>US</country></origination><destination><name>Jane
    Doe</name><address1>466 Tyende
    Ovi</address1><address2></address2><address3></
    address3><city>Flagstaff</city><state>AZ</state><postal>86001</
    postal><country>US</country></destination><package
    id="1"><boxID>1</boxID><weight unit="LB">10.0</weight><dimensions
    unit="IN"><length>10.0</length><width>10.0</width><height>10.0</
    height></dimensions><packaging>BOX</packaging><contents>OTR</
    contents><cod>18500</cod><insurance>15000</
    insurance><options><specialHandling
    id="1"><code>SDD</code><name>Saturday
    Delivery</name></specialHandling></options><quote
    id="1"><carrier><code>UPS</code><name>United Parcel
    Service</name><account>123456789</account><invoiced>1</invoiced></
    carrier><class><code>1DY</code><name>1st
    Day</name></class><service><code>NDA</code><name>Next Day
    Air</name></service><rate><amount>4201</amount><currency>USD</
    currency></rate></quote><quote
    id="2"><carrier><code>UPS</code><name>United Parcel
    Service</name><account>123456789</account><invoiced>1</invoiced></
    carrier><class><code>1DY</code><name>1st
    Day</name></class><service><code>NDS</code><name>Next Day Air
    Saver</name></service><rate><amount>3898</amount><currency>USD</
    currency></rate></quote><quote
    id="3"><carrier><code>UPS</code><name>United Parcel
    Service</name><account>123456789</account><invoiced>1</invoiced></
    carrier><class><code>1DY</code><name>1st
    Day</name></class><service><code>NDM</code><name>Next Day Air
    AM</name></service><rate><amount>7087</amount><currency>USD</
    currency></rate></quote></package></shipment>';

    if (!xml_p($xml_pr, $data, feof($fp))) {
    die(sprintf("XML error: %s at line %d",
    xml_error_string(xml_get_error_code($xml_pr)),
    xml_get_current_line_number($xml_pr)));
    }
    xml_pr_free($xml_pr);
    echo '</select></form>';


    /*
    Here is an example of the data returned from the XML pr

    SHIPMENT:
    VERSION: 2.0.0.0
    SHIPMENTID: 1234
    QUERYID: 23456
    DELIVERYTYPE: COM
    SHIPMETHOD: DRP
    ORIGINATION:
    NAME: John Smith
    ADDRESS1: 465 Tyende Ovi
    ADDRESS2:
    ADDRESS3:
    CITY: Flagstaff
    STATE: AZ
    POSTAL: 86001
    COUNTRY: US
    DESTINATION:
    NAME: Jane Doe
    ADDRESS1: 466 Tyende Ovi
    ADDRESS2:
    ADDRESS3:
    CITY: Flagstaff
    STATE: AZ
    POSTAL: 86001
    COUNTRY: US
    PACKAGE:
    BOXID: 1
    WEIGHT: 10.0
    DIMENSIONS:
    LENGTH: 10.0
    WIDTH: 10.0
    HEIGHT: 10.0
    PACKAGING: BOX
    CONTENTS: OTR
    COD: 18500
    INSURANCE: 15000
    OPTIONS:
    SPECIALHANDLING:
    CODE: SDD
    NAME: Saturday Delivery
    QUOTE:
    CARRIER:
    CODE: UPS
    NAME: United Parcel Service
    ACCOUNT: 123456789
    INVOICED: 1
    CLASS:
    CODE: 1DY
    NAME: 1st Day
    SERVICE:
    CODE: NDA
    NAME: Next Day Air
    RATE:
    AMOUNT: 4201
    CURRENCY: USD
    QUOTE:
    CARRIER:
    CODE: UPS
    NAME: United Parcel Service
    ACCOUNT: 123456789
    INVOICED: 1
    CLASS:
    CODE: 1DY
    NAME: 1st Day
    SERVICE:
    CODE: NDS
    NAME: Next Day Air Saver
    RATE:
    AMOUNT: 3898
    CURRENCY: USD
    QUOTE:
    CARRIER:
    CODE: UPS
    NAME: United Parcel Service
    ACCOUNT: 123456789
    INVOICED: 1
    CLASS:
    CODE: 1DY
    NAME: 1st Day
    SERVICE:
    CODE: NDM
    NAME: Next Day Air AM
    RATE:
    AMOUNT: 7087
    CURRENCY: USD
    */
    ?>


    I do apologize for the length of this email. I did my best to make it
    short.

    Doug La Farge Guest

  2. #2

    Default Re: [PHP] XML Parsing

    On Thu, 2003-07-17 at 10:03, Doug La Farge wrote:
    > Hello,
    >
    > I'm new to PHP but have been coding in Perl for over six years. I've
    > had a steep learning curve in the last few days. My task has been to
    > use fsockopen to pull in an XML stream, p that XML stream, and
    > package up that data into a complex, but usable data structure. We've
    > been able to accomplish this in Perl but now I'm attempting to port the
    > Perl code to PHP. The Perl code was written by Randal Schwartz (for me)
    > but he's not interested in helping me solve the PHP problems I'm
    > running into. I'm hoping to find someone on this list savvy in Perl
    > and PHP, and understands complex data structures (in perl the hash
    > refs/pointers go about four levels deep). I've included below the PHP
    > code with three functions in question. Below each function is the Perl
    > code I'm attempting to port.
    >
    > Here are some URL's that might help:
    >
    > [url]http://shop.sprocketdata.com/test.php[/url] - url for the code below (which
    > works if you copy paste)
    > ignore the feof error- i left out the fsockopen because I know that's
    > not where the problem is nor does this error effect the output.
    >
    > [url]http://stage.holdit.com/cgi-bin/is/shipping.cgi[/url] - url for equivalent
    > code in perl (ignore the drop-down)
    >
    > [url]http://stage.holdit.com/cgi-bin/is/fetch.cgi[/url] - the data structure I'm
    > trying to achieve in PHP
    >
    > [url]http://stage.holdit.com/cgi-bin/is/is.cgi[/url] - the raw XML data structure
    > - almost useless until pd and probably useless to look at :-).
    >
    > You'll also notice at the bottom of the code I've included how the PHP
    > XML pd the data. It's very similar to the fetch.cgi - maybe it's
    > different and thus causing the problem but I don't see it.
    >
    > <?php
    >
    > $state = array();
    > global $state;
    >
    > // these two associatative arrays will hold the data structure (I hope
    > :-)
    > $quote = array();
    > global $quote;
    > $quotes = array();
    > global $quotes;
    >
    > //XML start element
    > function startElement(&$Pr, &$Elem, &$Attr) {
    > global $state; //this works
    > array_push ($state, $Elem); //this works
    > $states = join (' ',$state); //can't interpolate the list so must
    > join - works
    > if ($states = "SHIPMENT PACKAGE") { //we make it this far
    if( $states == "SHIPMENT PACKAGE" ) {
    ^^
    > echo "$states\n"; //for testing should on print once!
    > // $package_id = $Attr->{id}; // how to write this in PHP?
    > }
    > //This conditional is never met - QUOTE gets popped off before it
    > returns
    > elseif ($states = "SHIPMENT PACKAGE QUOTE") { //why does perl see
    > QUOTE?
    elseif( $states == "SHIPMENT PACKAGE QUOTE" ){
    ^^
    > echo "$states\n"; // testing
    > // %quote = ( package_id => $package_id, id => $attr->{id} ); //Perl
    > // $quote = array ( package_id => $package_id, id => $attr->{id} );
    > //PHP version of above line?
    > }
    > }
    >
    > /*Perl
    > start_h => [ sub {
    > my ($tagname, $attr) = _;
    > push state, $tagname;
    > if ("state" eq "shipment package") {
    > print "state<br>";#testing
    > $package_id = $attr->{id};
    > }
    > elsif ("state" eq "shipment package quote") {
    > %quote = ( package_id => $package_id, id => $attr->{id} );
    > print "state<br>";#testing
    > }
    > }, "tagname, attr"],
    > */
    >
    > // XML data
    > //This function might work but I don't know yet as QUOTE never stays in
    > the $state stack
    > function characterData(&$Pr, &$Line) {
    > global $state;
    > $states = join (' ',$state);
    > if ($states = "SHIPPMENT ERROR") {
    if( $states == "SHIPMENT ERROR" ) {
    ^^
    > $error = $Line;
    > }
    > elseif ($states = "SHIPMENT PACKAGE QUOTE BOXID") {
    elseif( $states == "SHIPMENT PACKAGE QUOTE BOXID" ) {
    ^^

    > $boxID = $Line;
    > }
    > elseif ($states = "SHIPMENT PACKAGE QUOTE CARRIER NAME") {
    elseif( $states == "SHIPMENT PACKAGE QUOTE CARRIER NAME"
    ) {
    ^^
    > $quote{carrier_name} = $Line; // Doubting this will work - need to
    > fix this too
    > }
    > elseif ($states = "SHIPMENT PACKAGE QUOTE CARRIER CODE") {
    elseif( $states == "SHIPMENT PACKAGE QUOTE CARRIER CODE"
    ) {
    ^^
    > $quote{carrier_code} = $Line;
    > }
    > elseif ($states = "SHIPMENT PACKAGE QUOTE CLASS NAME") {
    elseif( $states == "SHIPMENT PACKAGE QUOTE CLASS NAME" )
    {
    ^^
    > $quote{class_name} = $Line;
    > }
    > elseif ($states = "SHIPMENT PACKAGE QUOTE CLASS CODE") {
    elseif( $states == "SHIPMENT PACKAGE QUOTE CLASS CODE" )
    {
    ^^
    > $quote{class_code} = $Line;
    > }
    > elseif ($states = "SHIPMENT PACKAGE QUOTE SERVICE NAME") {
    elseif( $states == "SHIPMENT PACKAGE QUOTE SERVICE NAME"
    ) {
    ^^
    > $quote{service_name} = $Line;
    > }
    > elseif ($states = "SHIPMENT PACKAGE QUOTE SERVICE CODE") {
    elseif( $states == "SHIPMENT PACKAGE QUOTE SERVICE CODE"
    ) {
    ^^
    > $quote{service_code} = $Line;
    > }
    > elseif ($states = "SHIPMENT PACKAGE QUOTE RATE AMOUNT") {
    elseif( $states == "SHIPMENT PACKAGE QUOTE RATE AMOUNT"
    ) {
    ^^
    > $quote{amount} = $Line;
    > }
    > }
    >
    > /* Perl
    > text_h => [ sub {
    > my ($text) = _;
    > if ("state" eq "shipment error") {
    > $error = $text;
    > }
    > elsif ("state" eq "shipment package boxID") {
    > $boxID = $text;
    > }
    > elsif ("state" eq "shipment package quote carrier name") {
    > $quote{carrier_name} = $text;
    > }
    > elsif ("state" eq "shipment package quote carrier code") {
    > $quote{carrier_code} = $text;
    > }
    > elsif ("state" eq "shipment package quote class name") {
    > $quote{class_name} = $text;
    > }
    > elsif ("state" eq "shipment package quote class code") {
    > $quote{class_code} = $text;
    > }
    > elsif ("state" eq "shipment package quote service name") {
    > $quote{service_name} = $text;
    > }
    > elsif ("state" eq "shipment package quote service code") {
    > $quote{service_code} = $text;
    > }
    > elsif ("state" eq "shipment package quote rate amount") {
    > $quote{amount} = $text;
    > }
    > }, "dtext"],
    > */
    >
    >
    > function endElement(&$Pr, &$Elem) {
    > global $state;
    > $states = join (' ',$state);
    > if ($states = "SHIPMENT PACKAGE QUOTE") {
    if( $states == "SHIPMENT PACKAGE QUOTE" ) {
    ^^
    > //echo "got QUOTE here $states\n";
    >
    > /*
    > The following three lines need porting to PHP
    > there is much perl magic here - I can explain if it doesn't
    > make sense. The short of this is it's a rather complex structure
    > Perhaps this is where things are breaking?
    > */
    > //$key = "quote{qw(carrier_code service_code)}";
    > //delete quote{qw(id package_id)}; # artificial
    > //$quotes{$boxID}{$key} = {%quote};
    > }
    > // obviously the part that pulls QUOTE off the $state stack
    > // perhaps a conditional?
    > array_pop($state);
    > }
    >
    > /*Perl
    > end_h => [ sub {
    > my ($tagname) = _;
    > if ("state" eq "shipment package quote") {
    > my $key = "quote{qw(carrier_code service_code)}";
    > delete quote{qw(id package_id)}; # artificial
    > $quotes{$boxID}{$key} = {%quote};
    > }
    > pop state;
    > }, "tagname"],
    > */
    >
    >
    >
    > //This code should work just fine
    > echo '<html><body><pre><form>';
    > $xml_pr = xml_pr_create();
    > xml_set_element_handler($xml_pr, "startElement", "endElement");
    > xml_set_character_data_handler($xml_pr, "characterData");
    >
    > //The XML data as returned from the server in it's raw form
    > $data = '<?xml version="1.0"?><shipment
    > xsi:schemaLocation="http://www.intershipper.com/Interface/Intershipper/
    > XML/v2.0/schemas response.xsd"
    > xmlns:xsi="http://www.w3.org/2001/XMLSchema-
    > instance"><version>2.0.0.0</version><shipmentID>1234</
    > shipmentID><queryID>23456</queryID><deliveryType>COM</
    > deliveryType><shipMethod>DRP</shipMethod><origination><name>John
    > Smith</name><address1>465 Tyende
    > Ovi</address1><address2></address2><address3></
    > address3><city>Flagstaff</city><state>AZ</state><postal>86001</
    > postal><country>US</country></origination><destination><name>Jane
    > Doe</name><address1>466 Tyende
    > Ovi</address1><address2></address2><address3></
    > address3><city>Flagstaff</city><state>AZ</state><postal>86001</
    > postal><country>US</country></destination><package
    > id="1"><boxID>1</boxID><weight unit="LB">10.0</weight><dimensions
    > unit="IN"><length>10.0</length><width>10.0</width><height>10.0</
    > height></dimensions><packaging>BOX</packaging><contents>OTR</
    > contents><cod>18500</cod><insurance>15000</
    > insurance><options><specialHandling
    > id="1"><code>SDD</code><name>Saturday
    > Delivery</name></specialHandling></options><quote
    > id="1"><carrier><code>UPS</code><name>United Parcel
    > Service</name><account>123456789</account><invoiced>1</invoiced></
    > carrier><class><code>1DY</code><name>1st
    > Day</name></class><service><code>NDA</code><name>Next Day
    > Air</name></service><rate><amount>4201</amount><currency>USD</
    > currency></rate></quote><quote
    > id="2"><carrier><code>UPS</code><name>United Parcel
    > Service</name><account>123456789</account><invoiced>1</invoiced></
    > carrier><class><code>1DY</code><name>1st
    > Day</name></class><service><code>NDS</code><name>Next Day Air
    > Saver</name></service><rate><amount>3898</amount><currency>USD</
    > currency></rate></quote><quote
    > id="3"><carrier><code>UPS</code><name>United Parcel
    > Service</name><account>123456789</account><invoiced>1</invoiced></
    > carrier><class><code>1DY</code><name>1st
    > Day</name></class><service><code>NDM</code><name>Next Day Air
    > AM</name></service><rate><amount>7087</amount><currency>USD</
    > currency></rate></quote></package></shipment>';
    >
    > if (!xml_p($xml_pr, $data, feof($fp))) {
    > die(sprintf("XML error: %s at line %d",
    > xml_error_string(xml_get_error_code($xml_pr)),
    > xml_get_current_line_number($xml_pr)));
    > }
    > xml_pr_free($xml_pr);
    > echo '</select></form>';
    >
    >
    > /*
    > Here is an example of the data returned from the XML pr
    >
    > SHIPMENT:
    > VERSION: 2.0.0.0
    > SHIPMENTID: 1234
    > QUERYID: 23456
    > DELIVERYTYPE: COM
    > SHIPMETHOD: DRP
    > ORIGINATION:
    > NAME: John Smith
    > ADDRESS1: 465 Tyende Ovi
    > ADDRESS2:
    > ADDRESS3:
    > CITY: Flagstaff
    > STATE: AZ
    > POSTAL: 86001
    > COUNTRY: US
    > DESTINATION:
    > NAME: Jane Doe
    > ADDRESS1: 466 Tyende Ovi
    > ADDRESS2:
    > ADDRESS3:
    > CITY: Flagstaff
    > STATE: AZ
    > POSTAL: 86001
    > COUNTRY: US
    > PACKAGE:
    > BOXID: 1
    > WEIGHT: 10.0
    > DIMENSIONS:
    > LENGTH: 10.0
    > WIDTH: 10.0
    > HEIGHT: 10.0
    > PACKAGING: BOX
    > CONTENTS: OTR
    > COD: 18500
    > INSURANCE: 15000
    > OPTIONS:
    > SPECIALHANDLING:
    > CODE: SDD
    > NAME: Saturday Delivery
    > QUOTE:
    > CARRIER:
    > CODE: UPS
    > NAME: United Parcel Service
    > ACCOUNT: 123456789
    > INVOICED: 1
    > CLASS:
    > CODE: 1DY
    > NAME: 1st Day
    > SERVICE:
    > CODE: NDA
    > NAME: Next Day Air
    > RATE:
    > AMOUNT: 4201
    > CURRENCY: USD
    > QUOTE:
    > CARRIER:
    > CODE: UPS
    > NAME: United Parcel Service
    > ACCOUNT: 123456789
    > INVOICED: 1
    > CLASS:
    > CODE: 1DY
    > NAME: 1st Day
    > SERVICE:
    > CODE: NDS
    > NAME: Next Day Air Saver
    > RATE:
    > AMOUNT: 3898
    > CURRENCY: USD
    > QUOTE:
    > CARRIER:
    > CODE: UPS
    > NAME: United Parcel Service
    > ACCOUNT: 123456789
    > INVOICED: 1
    > CLASS:
    > CODE: 1DY
    > NAME: 1st Day
    > SERVICE:
    > CODE: NDM
    > NAME: Next Day Air AM
    > RATE:
    > AMOUNT: 7087
    > CURRENCY: USD
    > */
    > ?>
    >
    >
    > I do apologize for the length of this email. I did my best to make it
    > short.
    --
    Tyler Lane <tlanelyrical.net>

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.1 (GNU/Linux)

    iD8DBQA/FuP+nV+u8RFxEIMRAs2dAJ9StLrQU6EgahwqligE8L6GwRcShQ CaAi0L
    cdPHF48InWjythdnCAJKZdk=
    =yX8G
    -----END PGP SIGNATURE-----

    Tyler Lane Guest

  3. #3

    Default 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_push</name>
    <examples>
    <example>
    <name>A way to do it</name>
    <code>
    $array = array("foo", "bar")
    $array = array_push($array, "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($array, "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 Guest

  4. #4

    Default Re: XML parsing

    In article <mr-9EE649.19505212092003news.fu-berlin.de>,
    Sandman <mrsandman.net> wrote:
    > 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_push</name>
    > <examples>
    > <example>
    > <name>A way to do it</name>
    > <code>
    > $array = array("foo", "bar")
    > $array = array_push($array, "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($array, "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.

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

    --
    Sandman[.net]
    Sandman Guest

  5. #5

    Default Re: XML parsing

    Sandman wrote on Monday 15 September 2003 00:38:
    >> <function>
    >> <name>array_push</name>
    >> <examples>
    >> <example>
    >> <name>A way to do it</name>
    >> <code>
    >> $array = array("foo", "bar")
    >> $array = array_push($array, "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($array, "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.
    >
    >
    > Is there really no one who has a simple solution to the above?
    >
    I didn't get your first post, just your own follow-up. Here's one solution:

    1. Get ActiveLink PHP XML Package from:

    [url]http://www.active-link.com/intranet/software.php[/url]
    OR
    [url]http://php-xml.sourceforge.net/[/url]

    2. It helps to read docs at this point.

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

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

    function convertXMLToArray($xml) {
    $arrNew = array();
    if(get_class($xml) != "xml" && get_class($xml) != "xmlbranch")
    return(false);
    else {
    if($xml->hasBranch()) {
    $branches = $xml->getBranches();
    foreach($branches as $branch) {
    $tagName = $branch->getTagName();
    if($branch->hasBranch())
    $arrNew[$tagName] = convertXMLToArray($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
    [url]www.active-link.com/intranet/[/url]
    Zurab Davitiani Guest

  6. #6

    Default Re: XML parsing

    Zurab Davitiani wrote:
    > 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.
    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.
    > Disclaimer: I am the author of ActiveLink PHP XML Package; the package is
    > available under LGPL.
    No harm in little self promotion. Nice job on the package.

    --
    Dado

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

    Dalibor Karlovic Guest

  7. #7

    Default Re: XML parsing

    In article <9edg31-qv1.ln1amelie.ofr.hr>,
    Dalibor Karlovic <dadokrizevci.info> wrote:
    > > 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.
    >
    > 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.
    Ok, please elaborate - any suggestions are welcome!

    --
    Sandman[.net]
    Sandman Guest

  8. #8

    Default Re: XML parsing

    Sandman wrote:

    I appolagize for the length of the post.
    >> 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.
    >
    > Ok, please elaborate - any suggestions are welcome!
    You do like this:

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

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

    xslt_free($oXsl);
    ?>

    This will p 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_push</name>
    <examples>
    <example>
    <name>a way to do it</name>
    <code>
    <![CDATA[
    $myArray = array("foo", "bar");
    $myArray = array_push($myArray, "goo");
    ]]>
    </code>
    </example>
    <example>
    <name>another way</name>
    <code>
    <![CDATA[
    $myArray = array("foo", "bar", "goo");
    $myArray = array_push($myArray, "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($myArray, "goo");
    ]]>
    </code>
    </example>
    </examples>
    </function>
    </functions>

    test.xsl

    <xsl:stylesheet 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="examples">
    <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_push</h1><div>a way to do it<pre><code>

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

    </code></pre></div><div>another way<pre><code>

    $myArray = array("foo", "bar", "goo");
    $myArray = array_push($myArray, "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($myArray, "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.
    Dalibor Karlovic Guest

  9. #9

    Default XML parsing

    I've just finished my first XML parsing in ActionScript.

    Nipping out to the nearest field to scream out LOUD!!!

    Back in 20 minutes...


    Mark Guest

  10. #10

    Default XML Parsing

    Hi all

    I am trying to p an XML doent, however I get extra encoding and tag
    information in my output to a flash form.

    I p an XML doent as:

    mydoc = XmlP(myxmldoc);

    and then in the form I have an input:

    <cfinput type="text" name"myinput" value="#mydoc.root.name.first#>

    when I preview the page however, the following appears in my input box:

    <?xml version="1.0" encoding="UTF-8"?>
    <first>Joel</first>

    Am I missing a step when it comes to parsing or displaying my info ??

    any suggestions welcome

    thanks all

    Joel

    skermajo_1 Guest

  11. #11

    Default Re: XML Parsing

    Hi

    Try like this:

    <cfoutput>
    <cfset x=#xmlsearch(mydoc,"/first")#>
    <input type="text" name="myinput" value="#x[1].xmltext#">
    </cfoutput>

    I think this solves your problem.
    vkunirs Guest

  12. #12

    Default Re: XML Parsing

    Thanks for the reply

    I am wondering though if there is no easier way to do this? Is the issue
    simply that I am trying to insert the data as values into a form? As using my
    method above a standard <cfoutput> to html works fine without extra things
    (except for when I put it inside an <a href=""> tag

    skermajo_1 Guest

  13. #13

    Default Re: XML Parsing

    Hello Joel,

    When you use XMLP, it creates an XML Doc object. To reference element
    values, you can simply use the following syntax:

    Example:

    the variable myxmldoc contains the following:

    <?xml version="1.0" encoding="UTF-8"?>
    <CustomerInfo>
    <Name>
    <First>Frank</First>
    <Last>Furter</Last>
    </Name>
    <Address1 type="home">9999 Appian Way</Address1>
    </CustomerInfo>

    Using the example above, you can p and reference the name elements like
    this:

    <cfset mydoc = XMLP(myxmldoc)>

    <cfoutput>
    The first name is: #mydoc.CustomerInfo.Name.First#<br>
    The last name is: #mydoc.CustomerInfo.Name.Last#<br>
    </cfoutput>

    If you are attempting to reference attributes of an XML element, such as the
    "type" attribute of Address1 element the syntax would be:

    <cfoutput>
    The type of Address is: #mydoc.CustomerInfo.Address1.XMLAttributes.type# <br>
    </cfoutput>

    Hope this helps,

    Mark

    markknox Guest

  14. #14

    Default xml parsing

    i have been looking all over the web trying to find out more about chaining
    xmls but i haven't had any luck. what i am trying to do is using xml lister
    built into flash which has a parameter where you put the address of the xml
    file. my problem is i have topics and each topics has catagories. i need to
    make an xml that by user selection of the topic it would p a new xml list
    to the parameter of the other lister so it would show catagories of that topic.
    so far i have had no luck finiding any information regarding what i am trying
    to do. i would really appreciate it if somone could help me find the right
    topic where i could investigate this a little more.

    mita981 Guest

Similar Threads

  1. How do i Parsing xml
    By Todd Dignan in forum Macromedia Flash Data Integration
    Replies: 3
    Last Post: June 21st, 12:51 AM
  2. PDF Parsing
    By Tamir_Noach@adobeforums.com in forum Adobe Acrobat SDK
    Replies: 1
    Last Post: December 31st, 07:04 PM
  3. Parsing URL
    By Mercedissa1@netscape.net in forum PERL Beginners
    Replies: 2
    Last Post: September 9th, 08:34 PM
  4. Parsing PHP
    By Nikhil G. Daddikar in forum PHP Development
    Replies: 1
    Last Post: July 27th, 05:46 PM
  5. [PHP] Parsing PHP
    By Peter James in forum PHP Development
    Replies: 0
    Last Post: July 26th, 07:11 AM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139