Guru Advice Requested...

S

Skeets

disclaimer - i'm an xml newb.

my new project requires me to parse through an xml document and
populate an html form with the values. this appears simple if the
number of elements were to stay constant, however, they don't. i might
have one <element> or three <element>.

i'm using php and i'm planning on using PHP's DOM XML to parse through
the document a (using foreach) to nab each value and store it into a
variable and use those variables to display input into my forms. of
course, i'll need to use foreach in order to create my form elements,
too.

my variables would be created as follows:

$element1
$elemtent2

where the numeral is created as i loop through the values.

does this sound reasonable? is their some kind of DOM XML magic that
is designed to handle this case with ease?

any help / guidance would be greatly appreciated.
 
J

jamie.ly

you could use an array
$elements = array()
array_push ( $elements, $elementValue );
numberOfElements = count( $elements )
etc.

Is this what you are asking? This should be simple enough.
 
F

fedro

Skeets said:
disclaimer - i'm an xml newb.

my new project requires me to parse through an xml document and
populate an html form with the values. this appears simple if the
number of elements were to stay constant, however, they don't. i might
have one <element> or three <element>.

i'm using php and i'm planning on using PHP's DOM XML to parse through
the document a (using foreach) to nab each value and store it into a
variable and use those variables to display input into my forms. of
course, i'll need to use foreach in order to create my form elements,
too.

my variables would be created as follows:

$element1
$elemtent2

where the numeral is created as i loop through the values.

does this sound reasonable? is their some kind of DOM XML magic that
is designed to handle this case with ease?

any help / guidance would be greatly appreciated.

i had nearly the same problem and solved it as follows:

first i load my XML-Data in an DOMDocument. Afterwards i label every
element with a numbered "name" tag using XPath-expression "//*".

that's it:

$xml = DOMDocument::load($_GET["xml"]);
$XPath = new DOMXPath($xml);
$r = $XPath->query("//*");
for ($i=0;$i<$r->length;$i++) {
$r->item($i)->setAttribute("name", $i);
}

For creating the form i recommend XSL.

Cheers,
Dominik

By sending the form you can recognize the data in the fields by that
name.
 
M

Martin Honnen

Skeets said:
i might
have one <element> or three <element>.

That is not a problem at all as there are methods like
$domDocument->getElementsByTagName('element')
or
$someElement->getElementsByTagName('element')
to give you a node list of those element nodes for further processing.
And there is the possibility to use XPath for more complicated selections.
my variables would be created as follows:

$element1
$elemtent2

where the numeral is created as i loop through the values.

Does not sound like an XML problem then, as said for the XML elements
you have a node list to process, other data structures you want to fill
then are not XML related and as already suggested PHP has arrays to
allow you to store data e.g.
$data = array();
$data[] = ...;
$data[] = ...;
will increase the length of the array with each assignment.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,001
Messages
2,570,255
Members
46,852
Latest member
CarlaDowle

Latest Threads

Top