How to preserve ID as attribute when converting XML file to associative array?



I need to find ID of an element, but somehow I am not able to display it through my code. Why ID isn't showing up? It's not an attribute?


PHP



<?php
error_reporting(-1);
$dom = new DOMDocument; // object validating XML and DTD
$dom->Load('xml.xml'); // loading file into object
if ($dom->validate()) { // valid xml
echo 'This document is valid!<br><br>';
process_xml(); // processing the xml
}
else { // invalid xml
echo 'The document is not valid!<br><br>';
process_xml(); // processing the xml even if it's invalid
}

function process_xml() {
$xml = simplexml_load_file('xml.xml'); // xml file to xml object
$json = json_encode($xml); // xml object to json object
$collection = json_decode($json, true); // json object to associative array
foreach($collection as $table_name=>$table_content) { // splitting collections into tables
echo 'Table: ' . $table_name . '<br>';
foreach($table_content as $row) { // splitting table into rows
foreach($row as $column) { // splitting cows into columns
if(is_array($column)) { // checking if column is array
foreach($column as $column_name=>$column_content) { // reading column name and content
if($column_name = '@attributes') { // extracting attributes of the element
echo 'Attributes:<br>';
foreach($column_content as $attribute_name=>$attribute_content) { // reading attributes name and content
echo $attribute_name . '=>' . $attribute_content . '<br>';
}
}
else { // just in case
echo $column_name . ' => ' . $column_content . '<br>';
}
}
echo '<br>';
}
else { // column is not array
echo $column . '<br>';
}
}
echo '<br>';
}
echo '<br>';
}
//echo '<br><br><br>';
//print_r($collection);
}
?>


XML



<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE dataset [
<!ELEMENT dataset (description, languages, usages, structures, standards)>
<!ELEMENT description (language+)>
<!ELEMENT language EMPTY>
<!ATTLIST language language_id ID #REQUIRED
lang IDREF #REQUIRED
usage IDREFS #IMPLIED
structure IDREFS #IMPLIED
standard IDREFS #IMPLIED>
<!ELEMENT languages (lang+)>
<!ELEMENT lang (#PCDATA)>
<!ATTLIST lang lid ID #REQUIRED>
<!ELEMENT usages (use+)>
<!ELEMENT use (#PCDATA)>
<!ATTLIST use uid ID #REQUIRED>
<!ELEMENT structures (struc+)>
<!ELEMENT struc (#PCDATA)>
<!ATTLIST struc sid ID #REQUIRED>
<!ELEMENT standards (stand+)>
<!ELEMENT stand (#PCDATA)>
<!ATTLIST stand stid ID #REQUIRED>
]>
<dataset>
<description>
<language language_id="_001" lang="l001" usage="u001 u002 u003 u004" structure="s001 s004" standard="st001" />
<language language_id="_002" lang="l002" usage="u001 u002" structure="s001 s004" standard="st001" />
<language language_id="_003" lang="l003" usage="u001 u003 u005 u006 u007 u008 u009" structure="s001 s002 s003 s004 s005 s006 s007" standard="st001" />
<language language_id="_004" lang="l004" usage="u001 u002 u003 u004" structure="s001 s002 s005 s006" standard="st001" />
<language language_id="_005" lang="l005" usage="u007 u008 u009" structure="s001 s002 s003 s006" standard="st001" />
<language language_id="_006" lang="l006" usage="u008 u009" structure="s001 s002 s004 s006" standard="st001" />
<language language_id="_007" lang="l007" usage="u001 u003 u009 u010 u011 u012" structure="s001 s002 s003 s006" standard="st001" />
</description>
<languages>
<lang lid="l001">C</lang>
<lang lid="l002">C++</lang>
<lang lid="l003">C#</lang>
<lang lid="l004">Java</lang>
<lang lid="l005">JavaScript</lang>
<lang lid="l006">PHP</lang>
<lang lid="l007">Python</lang>
</languages>
<usages>
<use uid="u001">Application</use>
<use uid="u002">System</use>
<use uid="u003">General</use>
<use uid="u004">Low-Level Operations</use>
<use uid="u005">RAD</use>
<use uid="u006">Business</use>
<use uid="u007">Client-Side</use>
<use uid="u008">Server-Side</use>
<use uid="u009">Web</use>
<use uid="u010">Scripting</use>
<use uid="u011">Artificial Intelligence</use>
<use uid="u012">Scientific Computing</use>
</usages>
<structures>
<struc sid="s001">Imperative</struc>
<struc sid="s002">Object-Oriented</struc>
<struc sid="s003">Functional</struc>
<struc sid="s004">Procedural</struc>
<struc sid="s005">Generic</struc>
<struc sid="s006">Reflective</struc>
<struc sid="s007">Event-Driven</struc>
</structures>
<standards>
<stand stid="st001">ANSI</stand>
<stand stid="st002">ISO</stand>
<stand stid="st003">De Facto</stand>
<stand stid="st004">EMCA</stand>
<stand stid="st005">N/A</stand>
</standards>
</dataset>


Output


This document is valid!



Table: description
Attributes:
language_id=>_001
lang=>l001
usage=>u001 u002 u003 u004
structure=>s001 s004
standard=>st001

Attributes:
language_id=>_002
lang=>l002
usage=>u001 u002
structure=>s001 s004
standard=>st001

Attributes:
language_id=>_003
lang=>l003
usage=>u001 u003 u005 u006 u007 u008 u009
structure=>s001 s002 s003 s004 s005 s006 s007
standard=>st001

Attributes:
language_id=>_004
lang=>l004
usage=>u001 u002 u003 u004
structure=>s001 s002 s005 s006
standard=>st001

Attributes:
language_id=>_005
lang=>l005
usage=>u007 u008 u009
structure=>s001 s002 s003 s006
standard=>st001

Attributes:
language_id=>_006
lang=>l006
usage=>u008 u009
structure=>s001 s002 s004 s006
standard=>st001

Attributes:
language_id=>_007
lang=>l007
usage=>u001 u003 u009 u010 u011 u012
structure=>s001 s002 s003 s006
standard=>st001



Table: languages
C
C++
C#
Java
JavaScript
PHP
Python


Table: usages
Application
System
General
Low-Level Operations
RAD
Business
Client-Side
Server-Side
Web
Scripting
Artificial Intelligence
Scientific Computing


Table: structures
Imperative
Object-Oriented
Functional
Procedural
Generic
Reflective
Event-Driven


Table: standards
ANSI
ISO
De Facto
EMCA
N/A

No comments:

Post a Comment