View Full Version : PHP XML challenge

01-16-2012, 05:45 PM
Hi All,

The following code outputs in XML format and its working fine;


// Start XML file, create parent node

$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);

// Opens a connection to a MySQL server

$connection=mysql_connect (localhost, $username, $password);
if (!$connection) { die('Not connected : ' . mysql_error());}

// Set the active MySQL database

$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());

// Select all the rows in the markers table

$query = "SELECT * FROM markers WHERE 1";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());

header("Content-type: text/xml");

// Iterate through the rows, adding XML nodes for each

while ($row = @mysql_fetch_assoc($result)){
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("type", $row['type']);

echo $dom->saveXML();

The output XML format:

<marker name="Apaapa" type="CA"/>
<marker name="Maami" type="CC"/>

I however want to use two tables and get the following XML output but
cannot find my way out:

<marker name="Apaapa" type="CA"/>
<marker name="Maami" type="CC"/>
-<line colour="#000" width="2">
<point lat="5.876435" lng="-0.847364"/>
<point lat="5.855505" lng="-0.456736"/>
-<line colour="#000" width="2">
<point lat="5.800000" lng="-0.877304"/>
<point lat="5.832321" lng="-0.874600"/>

Your help is mush appreciated.


01-16-2012, 08:06 PM
The code you've posted will generate the XML as shown in your first example of output. It won't, however, generate the code shown in the second example.

Please clarify - do you mean that you would like to know how to write PHP to generate the second XML example, regardless of table structure? Or do you want to know what the MySQL query should be? Or both? A little help please ... !

01-16-2012, 08:10 PM
The exact same way you did it for the marker elements to create the line elements. The point needs to be added to the created line instead of the root though.
Assuming that the point is queried out, you do so and create a line to wrap them with since that data appears to be constant. You append this result to the root.

01-17-2012, 09:26 AM
Thanks for your reply. I really want to know how to write PHP to generate the second XML example with a table name "distance".

That is all i want to know.


01-17-2012, 09:32 AM
The point needs to be added to the created line instead of the root though.

I appreciate your reply but can you help me build that on the current code with table name "distance"


01-17-2012, 07:13 PM
I'm not sure what your XML file will be used for,
but do you think your example above is OK, or
might it be malformed?

I would expect your XML to look more like this,
with closing tags, and nested without duplicated tags.

<marker name="Apaapa" type="CA"/>
<line colour="#000" width="2"></line>
<point1 lat="5.876435" lng="-0.847364"/></point1>
<point2 lat="5.855505" lng="-0.456736"/></point2>
<marker name="Maami" type="CC"/>
<line colour="#000" width="2"></line>
<point1 lat="5.800000" lng="-0.877304"/></point1>
<point2 lat="5.832321" lng="-0.874600"/></point2>

When it comes time to parse the XML file, it seems like
it would be easier to do with closed tags. Just a thought.


01-17-2012, 08:01 PM
mlseim has me totally confused on this one O.o
Yours is actually incorrect, you have closing tags for single sided tags. Although I am curious if the structure should be grouping the line and point into the level of the marker, but I can see it waiting until after assuming that it somehow connects the two associated markers.
As for duplicate tags, no such idea exists in XML. That will only be dictated by the schema or DTD in use enforcing the number of children to a particular element.

I'm willing to bet that the points are separate from the markers. If they become children to the markers themselves, group of marker would have to add points to many locations, and a single alteration could be a fairly big task to do with some duplication involved. Leaving points separate provides a central place for such modifications and could easily add and remove from there. Thinking from a di-graph perspective this would be particularly true.

As for the OP, no, not with that information I cannot. I would have to assume that your table would contain a minimum of four fields, originating lat/long, and destination lat/long. As long as it has this, then you can do this easily from a query. But without knowing the structure of how you are pulling out the points in relation to the lines, then no I can't be any help at this.

01-17-2012, 08:15 PM
Fou-Lu ... sorry, I was just thinking aloud. It looks like it would be hard to
parse the XML as shown in post #1. But then again, I'm not sure what the
data really is. If it's points between those two cities? But there are 4 points,
not 2. So I guess I'm confused myself. I just don't know what unclosed tags
will effect when it's parsed. That doesn't seem right to me. oh well.

01-17-2012, 08:29 PM
You can't have unclosed tags in XML at all, its not forgiving like HTML is. But those are not unclosed, they are single sided, so they are closed and opened within the same tag.

I don't know what the points represent either.

01-17-2012, 09:12 PM
"single-sided" ... I'm just amazed at how I learn new things every day. thanks.
I guess that's what this forum is all about.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum