Reputation:
I am looking for a way to parse something like this:
<xml>
<data>
<name>zyx</name>
<value>xyz<value>
</data>
<data>
<name>cba</name>
<value>abc</value>
</data>
</xml>
And turn it into this:
<SELECT>
<OPTION VALUE="xyz">zyx</OPTION>
<OPTION VALUE="abc">cba</OPTION>
</SELECT>
Using only php so that if a client was to look into my source code they would only see the select options instead of a js parser (which looks confusing and displays the location of the xml file being parsed. Is there a way to do this with php? and How? Using Variable Inputs?
function select($tid, $t, $xml, $name, $iid, $elname, $vname, $nname)
{
$file = file_get_contents("modules/xml/$xml.xml");
$select = new SimpleXMLElement($file);
return "
<TR>
<TD ID=\"$tid\">
$t
</TD>
<TD ID=\"$tid\">
<SELECT NAME=\"$name\">";
foreach($select->$elname as $options)
{
return"
<OPTION VALUE=\"$OPTIONs->value\">
$OPTIONs->name
</OPTION>\n";
}
return
}
Upvotes: 0
Views: 664
Reputation: 12433
Using the example for SimpleXMLElement()
at http://php.net/manual/en/simplexml.examples-basic.php
$xml = "
<xml>
<data>
<name>zyx</name>
<value>xyz</value>
</data>
<data>
<name>cba</name>
<value>abc</value>
</data>
</xml>";
$select = new SimpleXMLElement($xml);
echo "<select>";
foreach($select->data as $options){
echo "<option value='$options->value'>$options->name</option>";
}
echo "</select>";
which returns
<select>
<option value='xyz'>zyx</option>
<option value='abc'>cba</option>
</select>
edit
If you want to load an external .xml file, then you want to use simplexml_load_file()
- http://php.net/manual/en/function.simplexml-load-file.php
$select = simplexml_load_file('xmlFile.xml');
echo "<select>";
foreach($select->data as $options){
echo "<option value='$options->value'>$options->name</option>";
}
echo "</select>";
Upvotes: 1