?
Solved

XML to Object Mapper using php

Posted on 2013-11-04
5
Medium Priority
?
748 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

777 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