无法读取带有'-';在PHP中使用SimpleXMLElement

无法读取带有'-';在PHP中使用SimpleXMLElement,php,xml,Php,Xml,我正在尝试使用PHP代码读取带有带“-”标记的XML文件。我的XML文件如下所示: 当我在没有连字符的情况下读取headelement并在xml文件中进行更改时,它是可读的,但当连字符存在时,它是不可读的,并给出错误: (!)尖叫:已为忽略错误抑制 (!)分析错误:语法错误,myfile中意外出现“[” <root> <head-element> ... ... ... </head-element> <hea

我正在尝试使用PHP代码读取带有带“-”标记的XML文件。我的XML文件如下所示: 当我在没有连字符的情况下读取headelement并在xml文件中进行更改时,它是可读的,但当连字符存在时,它是不可读的,并给出错误:

(!)尖叫:已为忽略错误抑制 (!)分析错误:语法错误,myfile中意外出现“[”

<root>
   <head-element>
    ...
    ...
    ...
   </head-element>
    <head-element>
    ...
    ...
    ...
   </head-element>
</root>

有人能帮我吗?

您可以尝试使用以下方法访问xml节点:

$xml = new domDocument;         
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;   

$xml = simplexml_load_file('root.xml'); 
$sxe = new SimpleXMLElement($xml->asXML()); 

//the way you can read your custom nodes
$nodes = $sxe->{'head-element'}[0];
希望能有帮助

$xml = new domDocument;         
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;   

$xml = simplexml_load_file('root.xml'); 
$sxe = new SimpleXMLElement($xml->asXML()); 

//the way you can read your custom nodes
$nodes = $sxe->{'head-element'}[0];