0

:) Let's say that i have that code:

<sample number="1">TEXT</sample>

but sometimes it could be

<sample number"1"/>

Q: How to check if it's self closed or not ? Or I want to check if it's there TEXT within element sample

Note: I'm using that way to retrieve XML doc:

$content = @file_get_contents($url);
$xml = new SimpleXMLElement($content);
7
  • 3
    Why is it important to know exactly how the user wrote the syntax? Would you want to reject <sample number="1"></sample>? Commented Feb 25, 2011 at 1:01
  • 1
    This is not an answer, but... it's usually wrong to want to detect this, for pedantic semantic reasons. Just parse the XML in whatever [valid] form it comes, deal with the data, and move on. Commented Feb 25, 2011 at 1:02
  • No, I want to reject <sample number"1"/>. and retrieve data from <sample number="1">sample</sample> Commented Feb 25, 2011 at 1:02
  • I ask because, that data contain link to IMG,and when it's self closed -> no IMG, site looks ugly. I want to handle this. Commented Feb 25, 2011 at 1:03
  • 1
    @CappY: So you just want to determine if there is text within the element? Commented Feb 25, 2011 at 1:03

1 Answer 1

2

You need to type cast the element to string, then check if it's empty or not. Here's a quick example:

$test = simplexml_load_string("<test><elem test='12'><sub /><sub /></elem><elem test='12'>hi</elem><elem test='9' /><elem /></test>");
foreach($test as $elem){

    echo "\n";
    var_dump($elem);
    if((string)$elem == '' && $elem->count() == 0)
        echo 'Empty';
    else
        echo 'Full';


}

Will return:

object(SimpleXMLElement)#3 (2) {
  ["@attributes"]=>
  array(1) {
    ["test"]=>
    string(2) "12"
  }
  ["sub"]=>
  array(2) {
    [0]=>
    object(SimpleXMLElement)#4 (0) {
    }
    [1]=>
    object(SimpleXMLElement)#5 (0) {
    }
  }
}
Full
object(SimpleXMLElement)#5 (2) {
  ["@attributes"]=>
  array(1) {
    ["test"]=>
    string(2) "12"
  }
  [0]=>
  string(2) "hi"
}
Full
object(SimpleXMLElement)#3 (1) {
  ["@attributes"]=>
  array(1) {
    ["test"]=>
    string(1) "9"
  }
}
Empty
object(SimpleXMLElement)#5 (0) {
}
Empty
Sign up to request clarification or add additional context in comments.

3 Comments

That wasn't so clean solution for me, and after 1hour spending in searching cleaner solution i found that way works: if((string) $xml->sample != NULL) But thanks for answering. :)
I don't see how this might be cleaner. You're typecasting something to a string, and then compare it to something that is not a string. As you make a != and not a !==. php 'convert' the NULL to "". So basically, you're doing the same thing as me, except that you just check if their's text content, and not if their's child-elements.
@FMaz008's answer is perfectly fine for me, I don't see what's so unclear about it. In addition, your suggestion (string-casting and then comparing to null) doesn't make any sense and is misleading since string-casting will never result in null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.