简体   繁体   English

如何使用PHP解析具有多个根元素的XML?

[英]How can I parse an XML with multiple root elements using PHP?

I am trying to read this XML file using PHP and I have two root elements. 我试图使用PHP读取此XML文件,我有两个根元素。 The code that I wrote in PHP reads only one root element and when I add the other one ( <action> ) it gives me an error. 我在PHP中编写的代码只读取一个根元素,当我添加另一个( <action> )时,它给了我一个错误。 I want to do something like this : if($xml->action=="register") then print all parameters. 我想做这样的事情: if($xml->action=="register")然后打印所有参数。

This is my XML file: 这是我的XML文件:

<?xml version='1.0' encoding='ISO-8859-1'?>
<action>register</action>
<paramters>
    <name>Johnny B</name>
    <username>John</username>    
</paramters>

And this is my PHP script: 这是我的PHP脚本:

<?php
$xml = simplexml_load_file("test.xml");

echo $xml->getName() . "<br />";

foreach($xml->children() as $child)
{
    echo $child->getName() . ": " . $child . "<br />";
}
?>

I really don't know how to do all this... 我真的不知道怎么做这一切......

Fix your XML, it's invalid. 修复你的XML,它是无效的。 XML files can only have 1 root element. XML文件只能有1个根元素。

Example valid XML: 示例有效XML:

<?xml version='1.0' encoding='ISO-8859-1'?>
<action>
    <type>register</type>
    <name>Johnny B</name>
    <username>John</username>
</actions>

Or if you want only parameters to have own elements: 或者,如果您只希望参数具有自己的元素:

<?xml version='1.0' encoding='ISO-8859-1'?>
<action type="register">
    <name>Johnny B</name>
    <username>John</username>
</actions>

or if you want multiple actions: 或者如果你想要多个动作:

<?xml version='1.0' encoding='ISO-8859-1'?>
<actions>
    <action type="register">
        <name>Johnny B</name>
        <username>John</username>
    </action>
</actions>

EDIT : 编辑

As I've said in my comment, your teacher should fix his XML. 正如我在评论中所说,你的老师应该修复他的XML。 It is invalid . 无效 Also he should put his XML through a validator . 他还应该通过验证器提交他的XML。

If you're really desperate you can introduce an articificial root element, but this is really bad practice and should be avoided at all costs: 如果你真的很绝望,你可以引入一个人工根元素,但这是非常糟糕的做法,应该不惜一切代价避免:

$xmlstring = str_replace(
    array('<action>','</paramters>'),
    array('<root><action>', '</paramters></root>'),
    $xmlstring
);

None of the previous answers is quite accurate. 之前的答案都不是很准确。 The XML specification defines several kinds of entity: document entities, external parsed entities, document type definitions for example. XML规范定义了几种实体:例如文档实体,外部解析实体,文档类型定义。 Your example is not a well-formed document entity, which is what XML parsers are normally asked to parse. 您的示例不是格式良好的文档实体,通常要求XML解析器解析。 However, it is a well-formed external parsed entity. 但是,它是一个结构良好的外部解析实体。 The way to process a well-formed external parsed entity is to reference it from a skeletal document entity, like this: 处理格式良好的外部解析实体的方法是从骨架文档实体引用它,如下所示:

<!DOCTYPE wrapper [
<!ENTITY e SYSTEM "my.xml">
]>
<wrapper>&e;</wrapper>

and then pass the document entity to the XML parser. 然后将文档实体传递给XML解析器。

As it is an invalid xml file, you can do the following trick. 由于它是invalid xml文件,您可以执行以下操作。

  • Insert a dummy start tag at the second line as <dummy> 在第二行插入一个虚拟开始标记<dummy>
  • In the end finish it with </dummy> 最后用</dummy>完成它

Happy parsing ;) 快乐解析;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM