SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    xml no empty tags

    hi,

    1. get xml data from an api - done
    2. update xml data - done
    3. return the xml data to api - error

    i've been updating xml data using simplexml.
    here's my xml data
    Code XML:
    <data>
         <fname>john</fname>
         <lname>reyes</lname>
         <address>manila</address>
         <landline></landline>
         <mobile>09171234567</mobile>
    </data>

    here is the sample code for xml update

    Code PHP:
    header("content-type:application/xml");
    $xml = simplexml_load_file('data.xml', NULL, LIBXML_NOEMPTYTAG);
    //updating the address and mobilenumber
    //landline stays the same
    echo $xml = $xml->saveXML();

    now i have to return the xml data to the api but the api return a dtd validation because my php script to update xml data returns the tag landline as <landline/> not the original pair. i know this is a valid xml data but the api wants this kind of tag for empty tags -> <landline></landline>

    here is what the saveXML() method returned

    Code XML:
    <data>
         <fname>john</fname>
         <lname>reyes</lname>
         <address>baguio</address>
         <landline/>
         <mobile>09208651384</mobile>
    </data>

    i thought LIBXML_NOEMPTYTAG would be a help but i didn't.
    what i need now is a script to convert single empty tag (<landline/>) to empty pair tag (<landline></landline>)

    sorry for the xml technical terms, i hope you can help and understand me with my example. thanks..

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Before attempting this, are you sure that returning <landline></landline> with some altered data actually works also? I ask because while you imply it, you don't state this is a fact.

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is the actual php code i am running but without the data update.

    Code PHP:
    <?php
    header("content-type:application/xml");
    $xmlData = "<data>
         <fname>john</fname>
         <lname>reyes</lname>
         <address>manila</address>
         <landline></landline>
         <mobile>09171234567</mobile>
    </data>";
    $xml = simplexml_load_string($xmlData, NULL, LIBXML_NOEMPTYTAG);
    echo $xml = $xml->saveXML();

    and the return is

    Code XML:
    <?xml version="1.0"?>
    <data>
         <fname>john</fname>
         <lname>reyes</lname>
         <address>manila</address>
         <landline/>
         <mobile>09171234567</mobile>
    </data>

    asXML()/saveXML() i think has a bug or lacks in option for this empty tags as submitted here, http://bugs.php.net/bug.php?id=32307&edit=1
    now i want to ask for some work around on this problem. thanks..

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,070
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    You already said that.
    Could you answer the question from Cups?
    You might think you can solve the problem by doing this, while in actuality the problem lies somewhere else ...

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    The manual states...
    Note: This option is currently just available in the DOMDocument::save and DOMDocument::saveXML functions.
    However, it actually seems like you're after the LIBXML_NOBLANKS flag?
    LIBXML_NOBLANKS (integer)
    Remove blank nodes
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    However, it actually seems like you're after the LIBXML_NOBLANKS flag?
    Not at all, that is a different flag with results not particularly relevant to this topic (think along the lines of preserveWhiteSpace = FALSE [default is TRUE] with formatOutput = FALSE [default is FALSE] for DOM).

    Instead, just "change" the landline's value to an empty string or NULL.

    PHP Code:
    <?php

    $xml 
    '<data>
         <fname>john</fname>
         <lname>reyes</lname>
         <address>baguio</address>
         <landline/>
         <mobile>09208651384</mobile>
    </data>'
    ;

    $sxml = new SimpleXMLElement($xml);
    $sxml->address 'new address';
    $sxml->mobile 'new mobile';
    $sxml->landline ''// Magic happens

    header('Content-Type: application/xml');
    echo 
    $sxml->asXML();

    /*
    <?xml version="1.0"?>
    <data>
         <fname>john</fname>
         <lname>reyes</lname>
         <address>new address</address>
         <landline></landline>
         <mobile>new mobile</mobile>
    </data>
    */
    Salathe
    Software Developer and PHP Manual Author.

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Ah, thanks Salathe.

    However, the manual does state LIBXML_NOBLANKS "Removes blank nodes", awfully confusing as it obviously doesn't.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  8. #8
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Well it does, just not in the way that you were thinking. It refers to removing 'blank' nodes as in whitespace-only nodes between others (like you get with nicely formatted XML) rather thank 'blank' nodes as in named nodes with no content (like <sarcasm/>).

    I've had a play around and the 'empty string' workaround appears to work even for older versions of PHP (by that, I mean I tested it on one random older version: PHP 5.1.3) so hopefully it'll be fine for the OP. Do let us know if it is not suitable.
    Salathe
    Software Developer and PHP Manual Author.

  9. #9
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    Before attempting this, are you sure that returning <landline></landline> with some altered data actually works also? I ask because while you imply it, you don't state this is a fact.
    Yes the API would accept anything with complete pair of tags (<landline>2927936</landline>), with or without data. but returns a dtd validation for single closing tags (<landline/>). any other tags in the xml is also validated not only the landline tag.

    for Salathe:
    i appreciate your suggestion but the xml i have shown you is just the tip of the iceberg. the real xml file contains up to 5 child nodes and more than 50 tags. it would be very tedious to check all tags if they are empty and update with NULL content just to make sure it would return a pair of tag on empty data. but if there is no easier solution i might do it instead.

  10. #10
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,157
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    It might be easier to use something other than simplexml.

  11. #11
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    It's not really particularly tedious to loop over all empty nodes and add no content (that sounds weird out of context!) which would be a couple of lines. The worst part is having to do something that really should be unnecessary. On that note, how painful would it be to ask the XML provider to relax their validation? Perhaps that might be an 'easier' (and certainly more practical for everyone else using the XML) path to take.

    Also, going along Mittineague's route: DOMDocument::saveXML allows you to pass the LIBXML_NOEMPTYTAG flag.
    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

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