Advertisement

02.29.2008 at 06:17AM PST, ID: 23203713 | Points: 500
[x]
Attachment Details

MySQL to XML - Outputting XML-nodes based on "categories"

Tags: FIREFOX, IE, SAFARI, NETSCAPE, OPERA
I have a PHP script that extracts a whole "MySQL table" into a "XML file". It's very simple.

The script loops the rows within the MySQL table. And the output is: (see "CODESNIPPET 1:").

The actual code is: (see "CODESNIPPET 2:").

My question is:

On each row in my table, I want the option to assign a Category. That means that there is a FIELD that has a Category_ID or something, that will help the XML output to be more specific. Hence it will look moore like this: (CODESNIPPET: 3)

How can I best possible accomplish this, when thinking of architecture and logic? And what would be the best approach, creating an unique table for the category ID's? Any example or code would really be appreciated.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
<CODESNIPPET 1:>
 
<?xml version="1.0"?>
<root>
  <tablename>
    <dbCOLUMN1>George</dbCOLUMN1>
    <dbCOLUMN2>Jenkins</dbCOLUMN2>
    <dbCOLUMN3>Funky Chicken</dbCOLUMN3>
  </tablename>
  <tablename>
    <dbCOLUMN1>Maria</dbCOLUMN1>
    <dbCOLUMN2>Romana</dbCOLUMN2>
    <dbCOLUMN3>Mahatma Jira</dbCOLUMN3>
  </tablename>
</root>
 
</CODESNIPPET: 1>
---
<CODESNIPPET: 2>
 
	$table_id = 'TABLENAME';
	$query = "SELECT * FROM $table_id";
	$dbresult = mysql_query($query, $con);
 
	// creates new XML document
	$doc = new DomDocument('1.0');
 
	// creates root node
	$root = $doc->createElement('root');
	$root = $doc->appendChild($root);
 
	// process one row at a time
	while($row = mysql_fetch_assoc($dbresult)) {
 
	// adds node for each row
	$occ = $doc->createElement($table_id);
	$occ = $root->appendChild($occ);
 
	// adds child node for each field in each row
	foreach ($row as $fieldname => $fieldvalue) {
 
	$child = $doc->createElement($fieldname);
	$child = $occ->appendChild($child);
 
	$value = $doc->createTextNode($fieldvalue);
	$value = $child->appendChild($value);
 
	} // foreach
	} // while
 
	// gets full XML document from what above
	$xml_string = $doc->saveXML();
	$file_handle = fopen('THEFILE.xml','a');
	fwrite($file_handle,$xml_string);
	fclose($file_handle);
 
</CODESNIPPET: 2>
---
<CODESNIPPET: 3>
 
<?xml version="1.0"?>
<root>
  <tablename>
    <category male>
        <young>
            <dbCOLUMN1>George</dbCOLUMN1>
            <dbCOLUMN2>Jenkins</dbCOLUMN2>
            <dbCOLUMN3>Funky Chicken</dbCOLUMN3>
        </young>
    </category male>
  </tablename>
  <tablename>
    <category female>
            <dbCOLUMN1>Maria</dbCOLUMN1>
            <dbCOLUMN2>Romana</dbCOLUMN2>
            <dbCOLUMN3>Mahatma Jira</dbCOLUMN3>
    </category female>
  </tablename>
</root>
 
</CODESNIPPET: 3>
Start your free trial to view this solution
Question Stats
Zone: Web Development
Question Asked By: gladideg
Question Asked On: 02.29.2008
Participating Experts: 1
Points: 500
Views: 0
Translate:
Loading Advertisement...
02.29.2008 at 06:31AM PST, ID: 21013455

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.29.2008 at 06:36AM PST, ID: 21013508

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.29.2008 at 08:58AM PST, ID: 21015115

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
20080236-EE-VQP-29 / EE_QW_2_20070628