Solved

XML to Object Mapper using php

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

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 110

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

696 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