Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法将SimpleXMLElement序列化到Laravel作业_Php_Xml_Laravel_Xml Parsing_Simplexml - Fatal编程技术网

Php 无法将SimpleXMLElement序列化到Laravel作业

Php 无法将SimpleXMLElement序列化到Laravel作业,php,xml,laravel,xml-parsing,simplexml,Php,Xml,Laravel,Xml Parsing,Simplexml,所以,在做了一些研究之后,我成功地解析了一些XML,这些XML是通过Guzzle通过simplexml\u load\u string得到的。问题是,当我随后尝试使用以下代码为每个子级分派作业时,我得到了一个“不允许序列化'SimpleXMLElement'的错误” 因此,为了解决这个问题,我可以使用以下技巧将XML转换为数组 json_decode(json_encode($child)) 然而,虽然这确实意味着我可以将数据发送到新作业,但这确实意味着,就我所能做到的而言,我无法访问@att

所以,在做了一些研究之后,我成功地解析了一些XML,这些XML是通过Guzzle通过simplexml\u load\u string得到的。问题是,当我随后尝试使用以下代码为每个子级分派作业时,我得到了一个“不允许序列化'SimpleXMLElement'的错误”

因此,为了解决这个问题,我可以使用以下技巧将XML转换为数组

json_decode(json_encode($child))
然而,虽然这确实意味着我可以将数据发送到新作业,但这确实意味着,就我所能做到的而言,我无法访问@attributes。另一种选择是以下几点:

// ParentJob
    $parent = $this->getParent($keywords);
    foreach ($parent->children() as $child) {
        dispatch(new ProcessChild($child->asXML, true), $this->otherVar);
    }

// ChildJob
    public function __construct($xml, $otherVar)
    {
        $this->xml = simplexml_load_string($xml);
        $this->otherVar = $otherVar;
    }
但是,由于某种原因,它仍然会在调度时抛出序列化错误,我无法解决,因为它应该只发送原始XML,而不是对象

所以我的主要问题是,在Laravel 5.3中,传递和传递SimpleXMLObject到一份工作的正确方式是什么


(除了像循环遍历所有节点/属性并从中构建我自己的集合之类的东西)

可以将
simplexmlement
转换为数组,如下所示:

$xml = <<<'XML'
<root>
  <x a="a1">1</x>
  <y b="b2">2</y>
  <z>3</z>
</root>
XML;

$xe = simplexml_load_string($xml);
$a = $xe->xpath('*');
$a = array_map(function ($e) {
  $item = (array) $e;
  $item['nodeName'] = $e->getName();
  return $item;
}, $a);
// Now `$a` is an array (serializable object)
echo json_encode($a, JSON_PRETTY_PRINT);
注意,您可以通过将
simplexmlement
强制转换为字符串来获取其字符串值:

$item['value'] = (string) $e;
由于
xpath
方法支持相对的xpath表达式,因此星号甚至可以用于名称空间的xml。考虑使用<代码> DOM>代码>扩展,因为它比<代码> SimpleXML < /代码>更灵活。特别是,它的类允许在XPath表达式中使用名称空间和注册的标识符:

$xpath->registerNamespace('myprefix', 'http://example.com/ns');
$xpath->query('/root/myprefix:*');

以这种方式将XML转换为JSON意味着丢失数据。如果可能的话,我建议保留XML

simplexmlement::asXML()
是一种方法。别忘了括号

$parent = $this->getParent($keywords);
foreach ($parent->children() as $child) {
    dispatch(new ProcessChild($child->asXML(), true), $this->otherVar);
}
将其作为属性调用意味着SimpleXML尝试将其解释为子元素节点。这意味着它将是一个(空)SimpleXMLElement

下面是一个显示行为的小示例:

$node = new SimpleXMLElement('<foo/>');
var_dump($node->asXml);
var_dump($node->asXml->getName());
var_dump($node->asXml());
$node=新的SimpleXMLElement(“”);
变量转储($node->asXml);
var_dump($node->asXml->getName());
var_dump($node->asXml());
输出:

object(SimpleXMLElement)#2 (0) {
}
string(0) ""
string(29) "<?xml version="1.0"?>
<foo/>
"
object(simplexmlement)#2(0){
}
字符串(0)”
字符串(29)”
"

事实证明,它无法工作的全部原因是我在子作业构造函数上使用simplexml\u load\u string(),这在作业实际序列化并推送到队列之前将其转换为simpleXMLElement。正确的方法是解析handle方法上的XML字符串,这是在从队列中提取作业进行实际处理之后完成的

现在我可以使用$child->asXML简单地分派子作业,并在实际处理作业时对其进行解析,这意味着我仍然可以使用所有漂亮的simpleXML特性,例如attributes()

工作示例:

foreach ($parent->children() as $child) {
    dispatch(new ProcessChild($event, true), $this->otherVar);
}
工作示例:

protected $xml;
protected $otherVar;

public function __construct($xml, $otherVar)
{
    $this->xml = $xml;
    $this->otherVar = $otherVar;
}

public function handle()
{
    $child = simplexml_load_string($this->xml);
    $attributes = $child->attributes();
}

“特别是,它的DOMXPath类允许注册名称空间并在XPath表达式中使用注册的标识符”。SimpleXML具有完全相同的功能,使用Right,但您必须记住,注册仅对对象有效,您在上调用了register*方法。这意味着在SimpleXML中,您必须一次又一次地执行此操作。$xml通过($child->asXML())传递给子作业。但是我不能使用
simplexml\u load\u字符串($this->xml)。它表示不允许对“SimpleXMLElement”进行相同的错误序列化。你知道这件事吗?
foreach ($parent->children() as $child) {
    dispatch(new ProcessChild($event, true), $this->otherVar);
}
protected $xml;
protected $otherVar;

public function __construct($xml, $otherVar)
{
    $this->xml = $xml;
    $this->otherVar = $otherVar;
}

public function handle()
{
    $child = simplexml_load_string($this->xml);
    $attributes = $child->attributes();
}