使用php

时间:2017-04-06 10:18:55

标签: php xml simplexml simplexml-load-string

基本情况是我正在编写一个xml文件,我需要一个名为as的属性,该属性中没有值,所以显示为我想要的

代码如下,这是php.net中给出的基本示例

<?php

include 'example.php';

$sxe = new SimpleXMLElement($xmlstr);
$sxe->addAttribute('type', 'documentary');

$movie = $sxe->addChild('movie');
$movie->addChild('title', 'PHP2: More Parser Stories');
$movie->addChild('plot', 'This is all about the people who make it work.');

$characters = $movie->addChild('characters');
$character  = $characters->addChild('character');
$character->addChild('name', 'Mr. Parser');
$character->addChild('actor', 'John Doe');

$rating = $movie->addChild('rating', '5');
$rating->addAttribute('type', 'stars');

$movie->addChild('test');

echo $sxe->asXML();

?>

结果是

        <?xml version="1.0" standalone="yes"?>
<movies type="documentary">
 <movie>
  <title>PHP: Behind the Parser</title>
  <characters>
   <character>
    <name>Ms. Coder</name>
    <actor>Onlivia Actora</actor>
   </character>
   <character>
    <name>Mr. Coder</name>
    <actor>El Act&#xD3;r</actor>
   </character>
  </characters>
  <plot>
   So, this language. It's like, a programming language. Or is it a
   scripting language? All is revealed in this thrilling horror spoof
   of a documentary.
  </plot>
  <great-lines>
   <line>PHP solves all my web problems</line>
  </great-lines>
  <rating type="thumbs">7</rating>
  <rating type="stars">5</rating>
 </movie>
<movie><title>PHP2: More Parser Stories</title><plot>This is all about the people who make it work.</plot><characters><character><name>Mr. Parser</name><actor>John Doe</actor></character></characters><rating type="stars">5</rating><test/></movie></movies>

但我需要它

        <?xml version="1.0" standalone="yes"?>
<movies type="documentary">
 <movie>
  <title>PHP: Behind the Parser</title>
  <characters>
   <character>
    <name>Ms. Coder</name>
    <actor>Onlivia Actora</actor>
   </character>
   <character>
    <name>Mr. Coder</name>
    <actor>El Act&#xD3;r</actor>
   </character>
  </characters>
  <plot>
   So, this language. It's like, a programming language. Or is it a
   scripting language? All is revealed in this thrilling horror spoof
   of a documentary.
  </plot>
  <great-lines>
   <line>PHP solves all my web problems</line>
  </great-lines>
  <rating type="thumbs">7</rating>
  <rating type="stars">5</rating>
 </movie>
<movie><title>PHP2: More Parser Stories</title><plot>This is all about the people who make it work.</plot><characters><character><name>Mr. Parser</name><actor>John Doe</actor></character></characters><rating type="stars">5</rating>**<test></test>**</movie></movies>

我已经看到很多其他问题,但他们使用的是DomDocument,我是xml的新手,所以无法弄清楚我在代码中如何做同样的事情。 我见过的参考文献是 question 1

question 2

我完全陷入困境!!

3 个答案:

答案 0 :(得分:2)

如果你真的需要像<node></node>这样的空节点,请执行以下操作:

 $child = $sxe->addChild('test');
 $child[0] = '';

现在该节点为空,但显示为<text></text>;

 print htmlentities($sxe->asXML());

答案 1 :(得分:2)

在XML中,['example', 'example', '000', 'example']<test></test>完全等效。据我所知,处理器可以随意重写另一个。

因此,当您操纵文档的树形表示时,但当您将其写入字符串时,不会发生在它们之间进行选择的操作。

当使用<test/>保存XML时,SimpleXML目前不支持标记,但DOM等效(->asXML()->save())可以。幸运的是,DOM和SimpleXML使用相同的内部表示,因此您可以从一个转换为另一个而不会降低性能。

您想要的标记列在http://php.net/manual/en/libxml.constants.php

  

LIBXML_NOEMPTYTAG   展开空标记(例如->saveXML()<br/>

要使用它,你会写:

<br></br>

虽然您可能会欺骗XML库创建一个默认情况下序列化为$dom = dom_import_simplexml($sxe); echo $dom->saveXML($dom->documentElement, LIBXML_NOEMPTYTAG); 的树结构(例如零长度文本节点),但这很容易被新版本的图书馆优化你的技巧。如果您将XML保存为字符串并再次解析它,差异也将消失。

答案 2 :(得分:1)

对于使用

的用户
$xml = new DOMDocument('1.0', 'utf-8');

@JustOnUnderMillions答案代替了SimpleXML,给了我 致命错误:无法将DOMElement类型的对象用作数组中的...

我发现设置对象的'nodeValue'对我有用:

$child = $parent->appendChild($dom->createElement('test')); 
$child->nodeValue = '';