Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End How to display text from XML between special characters

  • This topic is empty.
Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #185219
    digitalvaldosta
    Participant

    I am trying to display text from an XML file that is surrounded by special characters. I can get everything in the tag to display with simpleXML. It contains content that is unneeded.

    <Playlist>
    <Song id="1">Wed 01:49PM M7572|I AM YOURS|THE AFTERS|327</Song>
    </Playlist>
    <code></code>

    As in the above code example, I am trying to get the song title and artist (Title: I AM YOURS, Artist: THE AFTERS). This is dynamically generated content from an RDS system at a radio station. So I can’t change the format of the XML file.

    Any help would be great. Thanks

    #185246
    __
    Participant
    1. Are those actually html entities, or are they supposed to be XML tags (i.e., is it really & lt; or is it actually <)?
    2. What “special characters” are you referring to? What do you want the output too look like?
    #185260
    digitalvaldosta
    Participant

    The xml code:

    <Playlist>
    <Song id="1">Wed 01:49PM M7572|I AM YOURS|THE AFTERS|327</Song>
    </Playlist>

    For the full xml:
    http://goo.gl/NEl6cz

    I want to display:
    I am yours- The Afters

    I will be creating to pages:
    *One to display just the most recent entry in the file.
    *The other to display the ten most recent.

    Not sure what happened with my last post. Three code should have been wrapped in code tags

    #185263
    __
    Participant

    Okay, so once you get the full <Song> element, you’d need to split it on the pipes (|). You could also assign named keys to make access simpler:

    // $song is the value of the <song> element from your simpleXML object,
    // e.g., "Wed 01:49PM M7572|I AM YOURS|THE AFTERS|327"
    
    $songInfo = array_combine(
        array( 'datetime','title','artist','length' ),
        explode( $song,'|' );
    );
    
    print "{$songInfo['title']} - {$songInfo['artist']}";
    

    If you also want to change the case, you could do something like

    $title = ucwords( strtolower( $songInfo['title'] ) );
    

    Not sure what happened with my last post. Three code should have been wrapped in code tags

    Yeah, that happens once in a while.

    #185310
    digitalvaldosta
    Participant

    I’m still a noob to php (and programming at large), but here is the php code I have that “should” work to echo the first line of the XML file:

         <?php
                $mysongs = simplexml_load_file('WAFTHIS.xml');
                echo $mysongs->Playlist[0]->Song;
            ?>

    Either way all I get is a blank page. I added in an h3 element just so I know the page is loading. This bit of code worked with a previous file that I was using until I got a copy of the real xml file. You can see an example of that working here. Granted the two xml files have a different content I assumed that this should be about the same. At least with echo-ing the first tag of text.

    Also, I’m currently working local but will be accessing the xml file remotely at the link I posted previously. Which is why I am not using javascript (since due to security reasons javascript will not pull in files from a remote location).

    Thanks again for your help.
    [JOSH]

    #185316
    __
    Participant

    Either way all I get is a blank page. I added in an h3 element just so I know the page is loading.

    Meaning you’re getting no response at all? or do you see an empty h3 element?

    In either case, do you have error reporting enabled?

    Granted the two xml files have a different content I assumed that this should be about the same

    But do they have the same structure?

    #185324
    digitalvaldosta
    Participant

    Meaning you’re getting no response at all? or do you see an empty h3 element?

    No I hard coded the H3. So it shows what I put in there. I was talking about the output content from the php code you gave is not displaying. I altered the function names you put to match what I had (i.e., $song to $mysongs)

    But do they have the same structure?

    Not exactly as far as the tag names. But they both have a first tag with children (two levels). I borrowed the xml file from here in my working example.

    I was able to print to an array with this code:

    <?php
                $mysongs = simplexml_load_file('http://waftaudio.org/NOWPLAYING/WAFTHIS.XML');
        print_r($mysongs);
        ?>

    Not exactly what I’m looking for but at least the xml content is displayed. lol :-P

    As you can see, I have been researching and trying various methods. But open to trying anything that works.

    #185325
    __
    Participant

    No I hard coded the H3.

    I’m not sure what you mean by that. You put the <h3> tag before you do any of the simpleXML stuff?

    the output content from the php code you gave is not displaying.

    Well, you’ll either see output, or there will be an error. Do you have error reporting enabled?

    I borrowed the xml file from here in my working example.

    In general, don’t use w3schools. They’re not a great learning resource. The page you linked to, for example, demonstrates a lot of bad javascript practices. But you said you’re not using javascript anyway, didn’t you? We’ve been talking about php.

    I was able to print to an array with this code

    You can use that output to see the structure of the XML, so you know how to access the nodes you want. First, however, make sure error reporting is enabled and check your error logs.

    #185327
    digitalvaldosta
    Participant

    After trying various ways to enable error reporting, I found out that it already was by default. :-| lol

    So here are the most recent errors from my log:

    [Thu Oct 02 23:24:26 2014] [error] [client 127.0.0.1] PHP Notice: Trying to get property of non-object in /var/www/waft/index.php on line 14
    [Thu Oct 02 23:24:26 2014] [error] [client 127.0.0.1] PHP Notice: Undefined offset: 3 in /var/www/waft/index.php on line 22
    [Thu Oct 02 23:24:26 2014] [error] [client 127.0.0.1] PHP Notice: Undefined offset: 2 in /var/www/waft/index.php on line 22
    [Thu Oct 02 23:24:26 2014] [error] [client 127.0.0.1] PHP Notice: Undefined offset: 1 in /var/www/waft/index.php on line 22

    Line 22 is a closing </span> tag.
    Line 14 is
    `
    $song=simplexml_load_file(“http://waftaudio.org/NOWPLAYING/WAFTHIS.xml&#8221;);

    `
    I can only seem to get these errors to display in the logs. I have, however, seen previous errors when I misspelled the file name. But that has been a few days.

    I’m not sure what you mean by that. You put the <h3> tag before you do any of the simpleXML stuff?
    It’s irrelevant to the php. It’s plain html.

    In general, don’t use w3schools. They’re not a great learning resource. The page you linked to, for example, demonstrates a lot of bad javascript practices. But you said you’re not using javascript anyway, didn’t you? We’ve been talking about php.

    I was originally going to use javascript but it’s not possible for what is needed. This was the first example that I came across that gave a real xml file instead of something generated by the code. I figured it would help in my finding a solution. However, now I can just use the real file. Which I have updated my live example to show the live xml content.

    Well, I hope this information helps. Good night and thank you very much.
    [JOSH]

    #185345
    digitalvaldosta
    Participant

    Here is a link to the full code where you can fork or download it.

    #185352
    __
    Participant

    Start with the first error first (very often, it will be the cause of the subsequent errors).

    Notice: Trying to get property of non-object in /var/www/waft/index.php on line 14

    The “non-object” is your simpleXML object. If something should be an object, but PHP says it’s not, the usual cause is that construction of the object failed.

    In this case, SimpleXML failed because you gave it a URL instead of an XML string. Check the docs: $data can only be a URL when you set the $data_is_url flag to TRUE:

    $song = simplexml_load_file( “http://waftaudio.org/NOWPLAYING/WAFTHIS.xml”,0,TRUE );
    

    I can only seem to get these errors to display in the logs.

    You can change that in your php.ini file, or at runtime with
    ini_set( 'display_errors','on' );
    If you don’t want to, however, there’s no real need to do so. I find it more convenient to simply tail the error log.

    #185491
    digitalvaldosta
    Participant

    Check the docs: $data can only be a URL when you set the $data_is_url flag to TRUE:

    That was the issue here. Thanks.

    I still was having issues getting the code to work that you gave earlier (without error). This was weird, but could have something to do with the fact I am on Linux Mint which I have found serveral issues with across the whole system (I am in the process of moving to a more stable reliable OS soon – Debian Linux). But anyways.

    I took your advice about explode and used it with “list” (I saw this mentioned on several sites), and you can see the finished code here (http://codetidy.com/5383/). I also needed a page to list all of the contents of the XML file in a table which you have also helped me with, along side the assistance of another page I found (http://goo.gl/sHSMpE) about placing each child in a table row.

    Thank you very much for your help.

    #185496
    __
    Participant

    Thank you very much for your help.

    You’re welcome.

    Minor polishing.

    This was weird, but could have something to do with the fact I am on Linux Mint which I have found serveral issues with across the whole system

    What release? I had problems with 14, but 15 smoothed everything out. (I think they’re on 17 now.) The only time I’ve had problems with Mint since then is on machines with very little RAM or no graphics (it is one of the “heavier” distros).

    In any case, what sort of errors are you getting? It’s unlikely they stem from your OS specifically.

    #185507
    digitalvaldosta
    Participant

    So glad that we have a community of ppl that want to help others. :-)

    Tried others like stackexchange and those are kind of hard to follow in some cases. o.O

Viewing 14 posts - 1 through 14 (of 14 total)
  • The forum ‘Back End’ is closed to new topics and replies.