Possible Duplicate:
Get root node of XML doc using simplexml
QUESTIONs: I got some questions regarding XML parsing via SimpleXML. I still don't get around it completely.
I know it's bad habbit asking 3 Qs in one Thread, but i didn't want to bother everyone with that much repeating code samples.
I want to expend my knowledge, so every answer is welcome. Even if it comes to code styling or else. Thank you.
I have a xml file like the following:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE config_admin [
<!ELEMENT config_admin (page,sub_page)>
<!ELEMENT page (page_title,slug,menu_title)>
<!ELEMENT page_title (#PCDATA)>
<!ELEMENT slug (#PCDATA)>
<!ELEMENT menu_title (#PCDATA)>
]>
<config_admin>
<!--
MAIN Page
-->
<page>
<page_title>OOO</page_title>
<slug>ooo</slug>
<menu_title>OOO</menu_title>
<cap>manage_options</cap>
<icon_url>OOO</icon_url>
<position>OOO</position>
<tabs>
<abc_uid>abc</abc_uid>
<def_uid>def</def_uid>
<ghi_uid>ghi</ghi_uid>
</tabs>
<!-- Options for the main page -->
<opt_group>
<group_name>OOO</group_name>
<opt_name>OOO</opt_name>
<tab>ABC</tab>
<!-- Section -->
<section>
<section_title>OOO</section_title>
<section_UID>OOO</section_UID>
<!-- Defaults -->
<field_defaults>
<field_uid>Value</field_uid>
<_oxoxoxo>OXO</_oxoxoxo>
<_axaxaxa>AXA</_axaxaxa>
<_cxcxcxc>CXC</_cxcxcxc>
</field_defaults>
... etc.
...and parse it through using a class which does nothing than the following:
$data = new myXmlParser( $require_files_path.$file );
$data = (array) $data;
$config_data[] = $data;
echo '<pre>OUTPUT: <br />';
echo '===================================================================== <br />';
foreach ( $config_data as $key => $value) :
print_r($value);
echo '<br />';
endforeach;
echo '</pre>';
// The Class i use to parse
if ( !class_exists('myXmlParser') ) {
class myXmlParser {
public $file;
public $config_data;
public function __construct( $file ) {
$this->file = $file;
$this->config_data = simplexml_load_file( $this->file );
$this->config_data = $this->recursive_parse( $this->config_data );
}
private function recursive_parse( $data ) {
$output = array();
if ( is_object($data) ){
settype( $data, 'array' );
}
foreach ( $data as $key => $value ){
if ( $key == 'comment' )
unset($key);
if ( $key && $value ) :
if ( is_array($value) || is_object($value) ) {
$output[$key] = $this->recursive_parse( $value );
}
else {
$output[$key] = (string) $value;
}
endif;
}
return $output;
}
}
}
However, the output is like the following:
OUTPUT:
=====================================================================
Array
(
[file] => R:\development\xampp\htdocs\wordpress\wp-content\themes\child_test/config_admin.xml
[config_data] => Array
(
[page] => Array
(
[page_title] => OXO
[slug] => oxo
[menu_title] => OXO
[cap] => manage_options
[icon_url] => OXO
[position] => OXO
[tabs] => Array
(
[abc_uid] => abc
You should remove the extraneous questions from this question and post them separately. Keeping questions short makes it easier for readers to respond.
To answer your question
Where is my root element (config_admin) or how do i get it's name?
Your root element is returned by simplexml_load_string()
or simplexml_load_file()
. In case you don't know its name, you can get its name with getName()
. If you know its name, you should always name the variable the same name as the root element, as it makes it easier to match variables to the node they represent.
$config_admin = simplexml_load_file('config.xml');
echo $config_admin->getName();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With