Solved

XML to Object Mapper using php

Posted on 2013-11-04
5
644 Views
Last Modified: 2013-11-06
Here is my file `input.xml`

?xml version="1.0" ?> 
<document>
	<section id="6935f04648c3c3b47542a63ca4609cc7">
		<title>Start cover</title>
	</section>
	<section id="9f0c662896f9d254c1c265bc1b7af92d">
		<title>TOC</title>
	</section>
	<section id="f824d88cb5b7cef4cd861b2be72e04bb">
		<title>Argumentary</title>
		<chapter id="ebdddb985a739f84b58fcca1fb2ca378">
			<title>Chapter 1</title>
			<subchapter id="be91fade2e1cf544f93919b17d300e10">
				<title>Subchapter 1.1</title>
				<topic id="3b942fae1596a5e4a94c073ea040781e">
					<title>Topic A</title>
					<paragraph id="9ebae5de1075efb4699abaaf106acf57" level="1">Welldone</paragraph>
					<paragraph id="30ddac0f0c0de76479e64d16ed645873" level="2">not so good</paragraph>
					<paragraph id="1a53acd31aaa89d40172923ff2b20cb2" level="3">That look good.</paragraph>
					<paragraph id="a389dad1a9ddf9c4190cf6e2caf5bc25" level="1">yes it it.</paragraph>
				</topic>
				<topic id="9bafd300ad209e9489a4dd7acf0a6e06">
					<title>Topic B</title>
					<paragraph id="f65352c0e8ce90e4b990c3a0a8087968" level="1">Some text hello </paragraph>
					<paragraph id="e985b199564be424f52cf9d190bc42f2" level="1">some text.</paragraph>
				</topic>
			</subchapter>
</chapter>
</section>

Open in new window



The goal is to build "XML to Object Mapper"
Main page main.php is entirely generated from server side, including HTML for left (title) and right panels’ content.
Here we want each title to be considered as a same basic element having common properties and functionalities, but each element type (section, chapter, subchapter) having also its specificities.

Anyone could give some idea to achiev this goad?

Thanks all !
0
Comment
Question by:pelnisgproup
  • 2
5 Comments
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39623303
Please have a look at the SimpleXML PHP extension: http://www.php.net/manual/en/intro.simplexml.php


The SimpleXML extension provides a very simple and easily usable toolset to convert XML to an object that can be processed with normal property selectors and array iterators.

Is that what you're after?
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39623956
A couple of minor changes were needed to make the XML into a valid document.  Please see the example here that shows how to use the SimpleXML class.  You will want to use "view source" to inspect the browser output.
http://www.laprbass.com/RAY_temp_pelnisgroup.php

<?php // RAY_temp_pelnisgroup.php
error_reporting(E_ALL);
echo "<pre>";

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28284700.html

$xml = <<<EOD
<?xml version="1.0" ?>
<document>
    <section id="6935f04648c3c3b47542a63ca4609cc7">
        <title>Start cover</title>
    </section>
    <section id="9f0c662896f9d254c1c265bc1b7af92d">
        <title>TOC</title>
    </section>
    <section id="f824d88cb5b7cef4cd861b2be72e04bb">
        <title>Argumentary</title>
        <chapter id="ebdddb985a739f84b58fcca1fb2ca378">
            <title>Chapter 1</title>
            <subchapter id="be91fade2e1cf544f93919b17d300e10">
                <title>Subchapter 1.1</title>
                <topic id="3b942fae1596a5e4a94c073ea040781e">
                    <title>Topic A</title>
                    <paragraph id="9ebae5de1075efb4699abaaf106acf57" level="1">Welldone</paragraph>
                    <paragraph id="30ddac0f0c0de76479e64d16ed645873" level="2">not so good</paragraph>
                    <paragraph id="1a53acd31aaa89d40172923ff2b20cb2" level="3">That look good.</paragraph>
                    <paragraph id="a389dad1a9ddf9c4190cf6e2caf5bc25" level="1">yes it it.</paragraph>
                </topic>
                <topic id="9bafd300ad209e9489a4dd7acf0a6e06">
                    <title>Topic B</title>
                    <paragraph id="f65352c0e8ce90e4b990c3a0a8087968" level="1">Some text hello </paragraph>
                    <paragraph id="e985b199564be424f52cf9d190bc42f2" level="1">some text.</paragraph>
                </topic>
            </subchapter>
        </chapter>
    </section>
</document>
EOD;

// MAKE AN OBJECT
$obj = SimpleXML_Load_String($xml);

// SHOW HOW TO ACCESS SOME PROPERTIES OF THE OBJECT
foreach($obj->section as $sect)
{
    $sect_id    = $sect['id'];
    $sect_title = $sect->title;
    echo PHP_EOL . '<span id="' . $sect_id . '">' . $sect_title . '</span>';

    foreach ($sect->chapter as $chap)
    {
        foreach ($chap->subchapter->topic->paragraph as $para)
        {
            $para_id    = $para['id'];
            $para_level = $para['level'];
            echo PHP_EOL . '<span id="' . $para_id . '">' . "$para_level: $para". '</span>';
        }
    }
}

Open in new window

Regards, ~Ray
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39626714
@pelnisgroup: I see you are relatively new to Experts Exchange, and you may not understand the conventions and culture of the community.  Absent negligence or failure to offer help, the "default" grade for any answer to your question is "A" yet you gave a "C" in response to the answer here.  Please read the grading guidelines, shown here
http://support.experts-exchange.com/customer/portal/articles/481419

You received a tested and working code example that demonstrated the techniques.

I believe we are entitled to hear your explanation of why you gave the worst possible grade anyone can give to a colleague in this forum.  Why did you do that? ~Ray
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now