?
Solved

XML to Object Mapper using php

Posted on 2013-11-04
5
Medium Priority
?
808 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
3 Comments
 
LVL 43

Expert Comment

by:Rob
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 111

Accepted Solution

by:
Ray Paseur earned 1000 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 111

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 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