Jump to content
php.lv forumi

PHP SimpleXML empy value


omnipix
 Share

Recommended Posts

Sveiki,

 

Gribētu uzzināt kā ar PHP un SimpleXML validēt XML faila lauku kurš ir tukš?

mans XML:

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <CD_Albums>
        <cd-id>lg656363</cd-id>
        <cd-name>Lacim pofig</cd-name>
        <cd-author>Bomārs</cd-author>
        <cd-price>2.64LS</cd-price>
    </CD_Albums>

    <CD_Albums>
        <cd-id>bvbvbvbvbvbv</cd-id>
        <cd-name>Kurmis</cd-name>
        <cd-author>Zilais Varis</cd-author>
        <cd-price>6.73LS</cd-price>
    </CD_Albums>

    <CD_Albums>
        <cd-id>74646464664664</cd-id>
        <cd-name>Bebri ziemā krosu skrien</cd-name>
        <cd-author>Rižiks</cd-author>
        <cd-price></cd-price>
    </CD_Albums>
</root>

 

Kā redzams pēdejam ierakstam nav cena (empty value). Kā lai ar PHP es tam tieku klāt un attiecīgi ar if () {}esle{} to neparādu vai ko citu ar to izdaru. Ir kādas idejas?

 

 

Link to comment
Share on other sites

šitā:

$xml = simplexml_load_file('test.xml');
foreach ($xml->CD_Albums as $album)
{
  if (empty($album->{'cd-price'}))
  {
    // tukšs: darīt kaut ko
    echo 'empty: '.$album->{'cd-name'}.'<br/>';
  }
}

$album->cd-price nevar lietot, jo lauka nosaukumā ir mīnus zīme, tāpēc tiek lietots $album->{'cd-price'}

Edited by gurkjis
Link to comment
Share on other sites

šitā:

$xml = simplexml_load_file('test.xml');
foreach ($xml->CD_Albums as $album)
{
  if (empty($album->{'cd-price'}))
  {
    // tukšs: darīt kaut ko
    echo 'empty: '.$album->{'cd-name'}.'<br/>';
  }
}

$album->cd-price nevar lietot, jo lauka nosaukumā ir mīnus zīme, tāpēc tiek lietots $album->{'cd-price'}

 Paldies, viss aizgāja tagad!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...