9 Replies - 491 Views - Last Post: 02 March 2011 - 02:05 AM Rate Topic: -----

#1 mjay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-March 11

xml tags problem

Posted 01 March 2011 - 03:59 AM

I have wrote a php code for generate xml file using mysql database values.
I have attached here with m php file and generated xml file.
My xml closing tags placed on incorrect places.
XML FILE:
Spoiler


CODE:
Spoiler



Attached File  gendernew111.xml (48.95K)
Number of downloads: 31
Attached File  fillterdata.php (90.31K)
Number of downloads: 28

This post has been edited by JackOfAllTrades: 01 March 2011 - 04:44 AM
Reason for edit:: Spoilered wall of code


Is This A Good Question/Topic? 0
  • +

Replies To: xml tags problem

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: xml tags problem

Posted 01 March 2011 - 04:09 AM

Would you please narrow down your problem? Nobody is going to find an error in 2000 lines of code. Also, try formatting your XML. It will be much more readable and thus easier to debug.
Was This Post Helpful? 0
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3710
  • View blog
  • Posts: 5,958
  • Joined: 08-June 10

Re: xml tags problem

Posted 01 March 2011 - 04:15 AM

Hey.

You'll have to be a whole hell of a lot more specific than that before I wade through 2095 lines of code :)
Especially since the XML file you linked seems to be valid. (Firefox has no issues with it, at least.)

Two things I think I should point out though:
  • Strings should always be quoted. (As in, your case clauses.)
  • Why do you do all the switch statements in the first place? You can do:
    $f = $row['FromAnotherTable'];
    $sql = "SELECT stuff FROM mytable WHERE things = '{$f}'";
    
    

    You could probably even use a JOIN to trim it down even further.
    (Something to look into, at least.)

Was This Post Helpful? 0
  • +
  • -

#4 mjay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-March 11

Re: xml tags problem

Posted 01 March 2011 - 07:11 AM

thank you so much for the reply.

This is my xml code
<?xml version="1.0"  encoding="UTF-8"?>

<timeline swf_file="amxy.swf" settings_file="quachartsettings.xml" slider_x="60" slider_y="!35" slider_width="680" font="Arial" text_color="#dddddd" date_x="80%" date_y="72%" date_text_size="70" delay="0.1" loop="true" autoplay="false">

<data_set date="Jan"><chart><graphs><graph gid="0" title="Economy" color="0xFF6699"><point x="3.03233830845771" y="4.4652" value="Economy" bullet_color="0xFF6699">Planning for the long term future of the State</point><graph gid="0" title="Transport" color="0xFF6699"><point x="2.54975124378109" y="2.5281" value="Transport" bullet_color="0xFF6699">Handling industrial relations disputes</point>

<data_set date="Feb"><chart><graphs><graph gid="0" title="Economy" color="0xFF6699"><point x="3.03233830845771" y="4.4652" value="Economy" bullet_color="0xFF6699">Planning for the long term future of the State</point><graph gid="0" title="Transport" color="0xFF6699"><point x="2.54975124378109" y="2.5281" value="Transport" bullet_color="0xFF6699">Handling industrial relations disputes</point>

</graph></graph></graphs></chart></data_set>
</graph></graph></graphs></chart></data_set>
</timeline>


This post has been edited by macosxnerd101: 01 March 2011 - 09:12 AM
Reason for edit:: Please use code tags

Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,372
  • Joined: 27-December 08

Re: xml tags problem

Posted 01 March 2011 - 09:24 AM

Your XML validated fine for me, save for a couple warnings regarding not having a DTD, and assuming UTF-8 is being used. What problems or errors are you encountering?
Was This Post Helpful? 0
  • +
  • -

#6 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1002
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: xml tags problem

Posted 01 March 2011 - 01:03 PM

I'm afraid I don't see what errors you may be referring to... However may I suggest that you seriously optimise your code.

Having so many switch statements is useless and can easily be changed to something more manageable. You need to look into making your queries more dynamic, ie. more than $month being a variable.

Also, this only works by PHP's rubbishness:
case q2_1:


It should be case 'q2_1': as q2_1 is not a constant.

Furthermore, your variable declarations:
$v=1;
$k0=0;
$k1=0;
$k2=0;
$k3=0;
$k4=0;
$k5=0;
$k6=0;
$k7=0;
$k8=0;

$sn0=0;
$sn1=0;
$sn2=0;
$sn3=0;
$sn4=0;
$sn5=0;
$sn6=0;
$sn7=0;
$sn8=0;
$ab=0;
$mn=1; 


Think about whether they're really needed.. Even if they are, this can instantly be made more readable by doing this:
$k0 = $k1 = $k2 = $k3 = $k4 = $k5 = $k6 = $k7 = $k8 = 0;

$sn0 = $sn1 = $sn2 = $sn3 = $sn4 = $sn5 = $sn6 = $sn7 = $sn8 =0;

$v = 1;
$ab = 0;
$mn = 1;

Was This Post Helpful? 0
  • +
  • -

#7 mjay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-March 11

Re: xml tags problem

Posted 01 March 2011 - 07:35 PM

My xml closing tags has placed on wrong places.

I want xml code like this :


<data_set date="Jan"><chart><graphs><graph gid="0" title="Economy" color="0xFF6699"><point x="3.03233830845771" y="4.4652" value="Economy" bullet_color="0xFF6699">Planning for the long term future of the State</point></graph>
<graph gid="0" title="Transport" color="0xFF6699"><point x="2.54975124378109" y="2.5281" value="Transport" bullet_color="0xFF6699">Handling industrial relations disputes</point>
</graph></graphs></chart></data_set>


thank you.

I have wrote a php program for generate xml page using database values from mysql.
The problem is my xml closing tags has placed on wrong places.

I want xml code something like this :


<data_set date="Jan"><chart><graphs><graph gid="0" title="Economy" color="0xFF6699"><point x="3.03233830845771" y="4.4652" value="Economy" bullet_color="0xFF6699">Planning for the long term future of the State</point></graph>
<graph gid="0" title="Transport" color="0xFF6699"><point x="2.54975124378109" y="2.5281" value="Transport" bullet_color="0xFF6699">Handling industrial relations disputes</point>
</graph></graphs></chart></data_set>

Attached File(s)


This post has been edited by Valek: 01 March 2011 - 11:40 PM
Reason for edit:: Code tags. Please use them.

Was This Post Helpful? 0
  • +
  • -

#8 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: xml tags problem

Posted 01 March 2011 - 11:51 PM

There should be no reason for this script to be almost 2200 lines of code. I noticed inside your cases you're often setting the same variables to the same values as in other cases, you're executing the same functions and class methods with the same parameters as on other cases, etc. Your code will benefit and become infinitely more manageable by optimizing it. I'd start by declaring any variables you're going to need regardless of the case your switch enters to be declared before entering the switch. Then they're already initialized and ready. Also, any code you're going to reuse (for example, when creating your graph nodes, you could make a function that accepts the parameters title and color, and do your element creation that way. Since you're using that same code multiple times, what would you do if you had to make an adjustment to it? Right now, you'd have to modify every instance of creating a graph node. If it were done as a function, you'd modify the function, and every graph node's creation would be updated accordingly. Your code would also be smaller.

I would also recommend naming your variables better, so you have a better idea what you're looking at. If you were to walk away from this code and come back to it 6 months later, I doubt you'd be very interested in working on it, because you'll likely have forgotten what everything is/does, and you'll be overwhelmed by the fact that this is a 2200 line script that generates an XML file and has a lot of boilerplate in it.
Was This Post Helpful? 3
  • +
  • -

#9 mjay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-March 11

Re: xml tags problem

Posted 02 March 2011 - 01:39 AM

I have wrote a simple php program
<?php
$domtree = new DOMDocument('1.0', 'UTF-8');
		 $xmlRoot = $domtree->createElement("timeline");
		 $xmlRoot = $domtree->appendChild($xmlRoot);
			 
			 $mn=1;
			 
			 $ar=array("Jan","Feb","March","April","May","July","June","Aug","Sep","Oct","Nov","Dec");
			 $y=1;
			 $x=0;
			 while($y<13){
			 
			  $currentTrack1 = $domtree->createElement("data_set");
 $xmlRoot=  $xmlRoot->appendChild($currentTrack1);
$currentTrack1->setAttribute('date', $ar[$x]);
   $currentTrack2 = $domtree->createElement("chart");
  $xmlRoot =  $xmlRoot->appendChild($currentTrack2);
   $currentTrack3 = $domtree->createElement("graphs");
  $xmlRoot=  $xmlRoot->appendChild($currentTrack3);
			 
			 $x++;
			 $y++;
			 			 }
			 	 	 			 
						  echo $domtree->saveXML();
		  $domtree->save("mdata.xml"); 
			?>



This my generated xml file : "mdata.xml"
-------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<timeline>
<data_set date="Jan"><chart><graphs>
<data_set date="Feb"><chart><graphs>
<data_set date="March"><chart><graphs>
<data_set date="April"><chart><graphs>
<data_set date="May"><chart><graphs>
<data_set date="June"><chart><graphs>
<data_set date="July"><chart><graphs>
<data_set date="Aug"><chart><graphs>
<data_set date="Sep"><chart><graphs>
<data_set date="Oct"><chart><graphs>
<data_set date="Nov"><chart><graphs>
<data_set date="Dec"><chart><graphs>
</graphs></chart></data_set></graphs></chart></data_set></graphs></chart></data_set></graphs></chart></data_set></graphs></chart></data_set></graphs></chart></data_set></graphs></chart></data_set></graphs></chart></data_set></graphs></chart></data_set></graphs></chart></data_set></graphs></chart></data_set></graphs></chart></data_set>
</timeline>


I want to close my xml tag like this :
<data_set date="Jan"><chart><graphs></graphs></chart></data_set>

This post has been edited by Valek: 02 March 2011 - 01:47 AM
Reason for edit:: How many times do we have to tell you? Use the code tags!

Was This Post Helpful? 0
  • +
  • -

#10 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: xml tags problem

Posted 02 March 2011 - 02:05 AM

Problems I see in this code that I have corrected:

  • Your variable names were still rather unindicative of what they actually represent.
  • Your indentation was atrocious, which also results in unreadability.
  • You defined a variable which was never used ($mn), and a variable which was unnecessary ($x).
  • The root of your problem was a lack of understanding of how DOMNode::appendChild() works. The return value is the node you're adding, so every time you were setting $xmlRoot to the return value, you were endlessly appending your new nodes onto the node you had previously created, thus nesting them all.

This is why you should name your variables according to what they represent. As you create a new node, instead of naming it $currentTrack# (with # representing an incrementing number every time you create a new node), you should name it something more representative of what it actually is. This way, as you work through building your XML, you can tell exactly what pieces of it you're working with by simply looking at your variable names.

I would also highly recommend reading through the documentation for DOM operations.

<?php

$domtree = new DOMDocument('1.0', 'UTF-8');
$xmlRoot = $domtree->createElement("timeline");
$domtree->appendChild($xmlRoot);
$months = array("Jan","Feb","March","April","May","July","June","Aug","Sep","Oct","Nov","Dec");
$mcount = count($months);

for($y = 1; $y <= $mcount; $y++)
{
	$monthNode = $domtree->createElement("data_set");
	$xmlRoot->appendChild($monthNode);
	$monthNode->setAttribute('date', $months[$y - 1]);
	$chartNode = $domtree->createElement("chart");
	$monthNode->appendChild($chartNode);
	$graphsNode = $domtree->createElement("graphs");
	$chartNode->appendChild($graphsNode);
}

$domtree->save("mdata.xml"); 

?>

This post has been edited by Valek: 02 March 2011 - 02:07 AM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1