Results 1 to 5 of 5

Thread: PHP/simplexml only writes one tag if no data

  1. #1
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default PHP/simplexml only writes one tag if no data

    When the php writes back to the xml...if any of the fields (already existing or ones your php creates) have no data (I have some quotes that do not have references), instead of writing
    Code:
    <reference></reference>
    the php/simplexml writes
    Code:
    </reference>
    I use a Spry Data Set to place the sidebar on my .html pages but it's very picky about opening and closing tags.

    Is there an easy way to tell the code to add both tags?

    Code:
    $dcount = $_POST["dcount"]'
    
    $file = '../sidebar.xml';
    $xml = simplexml_load_file($file);
    
    $i = 0;
    foreach($xml as $kid){
      if(in_array($kid->id, $dcount)){
        unset($xml->myquotes[$i]);
        break;
      }
      $i++;
    }
    $xml = $xml->asXML($file);
    or is that something you have to put up with when using simplexml?

    my xml looks like...
    Code:
    <sidebar>
      <myquotes>
        <id></id>
        <thequote></thequote>
        <reference></thereference>
      </myquote>
    </sidebar>

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Just off the top of my head, couldn't you add some default content (like &nbsp; )? Kinda avoiding the issue, but sometimes that's easier.

  3. The Following User Says Thank You to traq For This Useful Post:

    I am Abby (05-06-2010)

  4. #3
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    I think it may be better to just do the job with simplexml. Spry Data Sets have other issues which made me do things a bit different than I normally would have.

  5. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Some testing: are you sure it's writing </reference> and not <reference/> ? I tried it out, and simpleXML seems to write self-closing tags when there's no content (likewise, Firefox -even though it doesn't rewrite the xml- only displays a self-closing tag when viewing xml with no content). You can still add content to them normally using simpleXML.

    I haven't found any documentation pointing towards a configuration option to prevent it, so if you need the opening & closing tags for some other application, I think you'd have to add placeholder content.

  6. #5
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    You are right...it's writing <reference/>
    Either way Spry does not like it. That's ok, I'm using what I learned here to put my sidbar on the pages.

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
  •