Solved

XML to Object Mapper using php

Posted on 2013-11-04
5
621 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
This article discusses how to create an extensible mechanism for linked drop downs.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

758 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

18 Experts available now in Live!

Get 1:1 Help Now