The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Parsing in PHP?

What are the options for parsing in PHP?
I've tried the xml parsing but figured it as data display only. I need to store the parsed data onto an array of object.
<row>
<field1>data11</field1>
<field2>data12</field2>
</row>
<row>
<field1>data21</field1>
.....
store it onto
$arr[1]->field1 = "data11";
$arr[1]->field2 = "data12";
$arr[2]->field1 = "data21";
....
How can I take advantage of the builtin xml parser in php?
thanks.
j2e
Tuesday, March 29, 2005
 
 
I didn't get it. Are you trying to read in just a list of values? You can try using an INI file: www.php.net/parse_ini_file
Pythonic Send private email
Tuesday, March 29, 2005
 
 
J Send private email
Tuesday, March 29, 2005
 
 
"How can I take advantage of the builtin xml parser in php?"

The DOM XML functions should be relatively easy to use.  Those functions load an XML document into a DOM (document object model) that you can query for all the records and fields.  It might take a little work to figure out but it's relatively straight forward.

(I can't help you more because I'm using PHP5 which has different DOM functions and capabilities).
spelling it out Send private email
Tuesday, March 29, 2005
 
 
Normally you'd use recursion, but basically:

    class row {
    var $field1;
    var $field2;
    }

    // parse the xml doc into a dom object
    $dom = domxml_open_mem($doc);

    // get the root node
    $root = $dom->document_element();

    $rows = array();

    // row children of the root node
    foreach ($root->get_elements_by_tagname('row') as $rowNode) {
    $newRow = new row;
        // field children of each row
    foreach ($rowNode->child_nodes() as $fieldNode) {
        if ($fieldNode->node_name() != "#text") {
        echo "Name: " . $fieldNode->node_name() . '<BR/>';
        $fieldName = $fieldNode->node_name();
                 
                  // dynamically assign to fields in class, check in real life
        $newRow->${fieldName} = $fieldNode->get_content();
        }
    }

    $rows[] = $newRow;
    }
    // don't forget to free the dom - odd behavior otherwise
    $dom->free();

    print_r($rows);
los Send private email
Wednesday, March 30, 2005
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz