Page 1 of 1

XML to array parser...

Posted: Thu Jan 24, 2008 9:33 am
by mattcooper
Hi all,

I'm working on a prototype markup schema based on XML that should allow any language to construct program logic from it. That's the idea, anyway; using markup to describe a program and letting programmers from different disciplines create interpreters/APIs for it seems to be a fairly logical approach to creating cross-platform code. However, I am a tad out of my depth with some of the concepts involved, most notably (and fundamentally!) XML parsing.

I've had a good scout about in phpclasses.org and found a couple of classes that looked promising, but turned out to be flawed in some way or not compatible with PHP5. Can anyone recommend or provide a parser that will produce an associative array and preserve attribute data etc, with minimum fuss and setup time (out of the box even)?

Incidentally, I would be interested to know if this kind of thing has been attempted or speculated about before, or if there is anything currently in development. Is anyone actually interested in this kind of approach to the cross-platform conundrum?

Cheers, look forward to hearing from you!

M :)

Re: XML to array parser...

Posted: Thu Jan 24, 2008 9:40 am
by Zoxive
Simplexml.

Its built into php5.

Re: XML to array parser...

Posted: Thu Jan 24, 2008 9:50 am
by mattcooper
That is simplicity itself! I'm a recent PHP5 convert, as you may have guessed ;). Many thanks.

Anyone got any comments on the other bit of my post?

Re: XML to array parser...

Posted: Thu Jan 24, 2008 5:41 pm
by Jonah Bron
The DOM. Simple connection:

Code: Select all

<?php
$xml = new DOMDocument();//initialize class
$xml->load('file.xml');//load xml file
$root = $xml->documentElement;//set root
$nodes = $root->getElementsByTagName('node_name');//find nodes called 'node_name'
foreach ($nodes as $node){//loop through each node
  echo $node->nodeValue;//echo value
  echo $node->getAttribute('attr');//echo 'attr' attribute
}
//echo particular node:
echo $nodes->item(0)->nodeValue;
?>
And some xml editing:

Code: Select all

<?php
$xml = new DOMDocument();
$xml->load('file.xml');
$nodes = $xml->documentElement->getElementsByTagName('node');//get nodes called 'node'
$newValue = $xml->createTextNode('New value for node');//create new value for node
$nowAttr = $xml->createAttribute('attr_name')->appendChild($xml->createTextNode('Value for new node'));//create new attribute already with value
$nodes->item(0)->appendChild($newValue);//append new value to node 1
$nodes->item(0)->appendChild($newAttr);//append new attr to node 1
$xml->save('file.xml');//don't forget to save!
?>
Note: item() is used the same as $array[0]

Re: XML to array parser...

Posted: Fri Jan 25, 2008 11:47 am
by mattcooper
Incidentally, I would be interested to know if this kind of thing has been attempted or speculated about before, or if there is anything currently in development. Is anyone actually interested in this kind of approach to the cross-platform conundrum?
Thanks, but the above is what I'm now interested in... new thread possibly?

Re: XML to array parser...

Posted: Sat Jan 26, 2008 3:00 pm
by dml
mattcooper wrote: ...That's the idea, anyway; using markup to describe a program and letting programmers from different disciplines create interpreters/APIs for it seems to be a fairly logical approach to creating cross-platform code. However, I am a tad out of my depth with some of the concepts involved, most notably (and fundamentally!) XML parsing.
...
Incidentally, I would be interested to know if this kind of thing has been attempted or speculated about before, or if there is anything currently in development. Is anyone actually interested in this kind of approach to the cross-platform conundrum?
Sure, looking at it very abstractly, what you're describing sounds like the standard distinction between a compiler front-end, which parses a language into an intermediate format, and a compiler back-end, which converts the intermediate format into platform-specific code. In that sense, even a language like C can be said to be a cross-platform markup that can be compiled in different ways for different platforms.

On the other end of a spectrum, people who constantly have to code up logic dealing with a very specific problem scope sometimes write "little languages" to describe those problems in a more expressive and compact way than their programming language is able to, and then write interpreters or compilers for those little languages.

The question is what problem you're trying to solve. Here's an example of somebody trying to solve the problem of being able to write their software once but being able to deploy it on both ASP and PHP.