Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP XML challenge

    Hi All,

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

    PHP Code:
    require("xml_dbcredential.php"); 

    // 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)){  
      
    // ADD TO XML DOCUMENT NODE  
      
    $node $dom->createElement("marker");  
      
    $newnode $parnode->appendChild($node);   
      
    $newnode->setAttribute("name",$row['name']); 
      
    $newnode->setAttribute("type"$row['type']);


    echo 
    $dom->saveXML(); 


    The output XML format:

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

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

    -<markers>
    <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>
    -<line colour="#000" width="2">
    <point lat="5.800000" lng="-0.877304"/>
    <point lat="5.832321" lng="-0.874600"/>
    </line>
    <markers>

    Your help is mush appreciated.

    Regards.
    Isaac

  • #2
    Regular Coder
    Join Date
    Jun 2010
    Posts
    293
    Thanks
    63
    Thanked 8 Times in 8 Posts
    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 ... !

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • #4
    New Coder
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Regards.

  • #5
    New Coder
    Join Date
    Jan 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    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"

    regards.

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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.

    Code:
    <markers>
       <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>
       <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>
        </marker>
    </markers>

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


    .

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • #8
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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.

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • #10
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    "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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •