在xml文档中存储布尔值并使用PHP SimpleXML读取

时间:2022-10-26 17:02:53

How do you store boolean values in an XML document such that you can read them using PHP's SimpleXML class?

如何在XML文档中存储布尔值,以便使用PHP的SimpleXML类读取它们?

I have the following XML document:

我有以下XML文档:

<?xml version='1.0' standalone='yes'?>
<status>
<investigating></investigating>
<log>
sample log
</log>
</status>

And the following php script to read it:

和下面的php脚本阅读:

if (file_exists($filename)) {
    $statusXml = simplexml_load_file($filename);
        if ($statusXml) {
            $investigating = (bool)($statusXml->investigating);
            echo $investigating;
        }
} else {
    exit('Failed to open ' . $filename .'.');
}

No matter what I put in the tag, it always gets read as true. I've tried "0", empty string, "false", and a bunch of other ideas but nothing has worked. I thought empty string should would because of what I found in this doc: http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting

不管我在标签里放什么,它总是被读成是真的。我尝试过“0”、空字符串、“false”,以及其他一些想法,但都没有成功。因为我在这个文档中找到了:http://www.php.net/manual/en/language.types.boolean.php# language.php # language.boolean.php # types.boolean.casting,所以我认为空字符串应该是空的

3 个解决方案

#1


6  

Considering that:

考虑到:

  1. XML data is always a string
  2. XML数据总是一个字符串
  3. PHP type casting rules can be pretty counter-intuitive
  4. PHP类型强制转换规则可能与直觉相反

... I would not rely on automatic type juggling. Simply, define a syntax you are comfortable with, cast to string when reading and write your own code to cast to boolean.

…我不会依赖自动类型杂耍。简单地说,定义一个您熟悉的语法,在读取时转换为字符串,并编写自己的代码转换为boolean。

<?php

function get_bool($value){
    switch( strtolower($value) ){
        case 'true': return true;
        case 'false': return false;
        default: return NULL;
    }
}

$data = '<?xml version="1.0"?>
<data>
    <empty_tag/>
    <true_tag>true</true_tag>
    <false_tag>false</false_tag>
</data>
';

$xml = simplexml_load_string($data);
var_dump( get_bool((string)$xml->empty_tag) );
var_dump( get_bool((string)$xml->true_tag) );
var_dump( get_bool((string)$xml->false_tag) );

Whatever, if you are curious about how to make (bool) work, you are basically dealing with objects so this is the rule that applies when casting to boolean:

无论如何,如果你对如何使(bool)工作感到好奇,你基本上是在处理对象,所以这是在布尔型时适用的规则:

  • SimpleXML objects created from empty tags: FALSE
  • 由空标签创建的SimpleXML对象:FALSE
  • Every other value: TRUE
  • 其他值:真的

That means that you will only get FALSE with <investigating /> (and TRUE with absolutely anything else). A trick I can think of to widen the range of possible TRUE values is to perform double casting:

这意味着您只会在< investigation />中得到FALSE(在其他任何情况下都是TRUE)。我能想到的扩大可能的真值范围的一个技巧是执行双铸造:

(bool)(string)$xml->investigating

Update: don't forget to debug with var_dump(). If you use echo you'll find that TRUE boolean casts to '1' string and FALSE to '' (empty string).

更新:不要忘记与var_dump()进行调试。如果您使用echo,您将发现TRUE boolean类型转换为'1'字符串,FALSE转换为'(空字符串)。

#2


-1  

You are not grabbing the right element.

你没有抓住正确的元素。

if (file_exists($filename)) {
    $statusXml = simplexml_load_file($filename);
    if ($statusXml = simplexml_load_file($filename)) {
        $investigating = (bool) $statusXml->investigating[0];
        echo $investigating;
    }
}

#3


-1  

This is quite old but just in case someone is looking for an answer. You could just use:

这是相当古老的,但只是以防有人在寻找答案。你可以使用:

<example is-true=1>

or

<example is-true=0>

This should work fine.

这应该工作很好。

Note using <example is-true="0"> will return true if you use an an if statement on it.

注意,如果使用if语句,使用 将返回true。

#1


6  

Considering that:

考虑到:

  1. XML data is always a string
  2. XML数据总是一个字符串
  3. PHP type casting rules can be pretty counter-intuitive
  4. PHP类型强制转换规则可能与直觉相反

... I would not rely on automatic type juggling. Simply, define a syntax you are comfortable with, cast to string when reading and write your own code to cast to boolean.

…我不会依赖自动类型杂耍。简单地说,定义一个您熟悉的语法,在读取时转换为字符串,并编写自己的代码转换为boolean。

<?php

function get_bool($value){
    switch( strtolower($value) ){
        case 'true': return true;
        case 'false': return false;
        default: return NULL;
    }
}

$data = '<?xml version="1.0"?>
<data>
    <empty_tag/>
    <true_tag>true</true_tag>
    <false_tag>false</false_tag>
</data>
';

$xml = simplexml_load_string($data);
var_dump( get_bool((string)$xml->empty_tag) );
var_dump( get_bool((string)$xml->true_tag) );
var_dump( get_bool((string)$xml->false_tag) );

Whatever, if you are curious about how to make (bool) work, you are basically dealing with objects so this is the rule that applies when casting to boolean:

无论如何,如果你对如何使(bool)工作感到好奇,你基本上是在处理对象,所以这是在布尔型时适用的规则:

  • SimpleXML objects created from empty tags: FALSE
  • 由空标签创建的SimpleXML对象:FALSE
  • Every other value: TRUE
  • 其他值:真的

That means that you will only get FALSE with <investigating /> (and TRUE with absolutely anything else). A trick I can think of to widen the range of possible TRUE values is to perform double casting:

这意味着您只会在< investigation />中得到FALSE(在其他任何情况下都是TRUE)。我能想到的扩大可能的真值范围的一个技巧是执行双铸造:

(bool)(string)$xml->investigating

Update: don't forget to debug with var_dump(). If you use echo you'll find that TRUE boolean casts to '1' string and FALSE to '' (empty string).

更新:不要忘记与var_dump()进行调试。如果您使用echo,您将发现TRUE boolean类型转换为'1'字符串,FALSE转换为'(空字符串)。

#2


-1  

You are not grabbing the right element.

你没有抓住正确的元素。

if (file_exists($filename)) {
    $statusXml = simplexml_load_file($filename);
    if ($statusXml = simplexml_load_file($filename)) {
        $investigating = (bool) $statusXml->investigating[0];
        echo $investigating;
    }
}

#3


-1  

This is quite old but just in case someone is looking for an answer. You could just use:

这是相当古老的,但只是以防有人在寻找答案。你可以使用:

<example is-true=1>

or

<example is-true=0>

This should work fine.

这应该工作很好。

Note using <example is-true="0"> will return true if you use an an if statement on it.

注意,如果使用if语句,使用 将返回true。