xml中的元素中带有点字符

xml中的元素中带有点字符

本文介绍了php simplexml,xml中的元素中带有点字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用以下xml格式,如何从php中的XMLReader访问News.Env元素?

With the below xml format how we can access News.Env element from XMLReader in php?

$xmlobj->News->News.Env给出了不正确的Env.

$xmlobj->News->News.Env gives Env which is not correct.

<?xml version="1.0" encoding="utf-8"?>
<News>
  <News.Env>abc</News.Env>
</News>

推荐答案

这是因为点.是php中的字符串连接器.在您的情况下,它尝试将$xmlobj->News->News(不存在,因此为空)与常量Env(也不存在,并被视为字符串)连接.适当的error_level)

This is because the dot . is the string concatenator in php. In your case it tries to concatenate $xmlobj->News->News (which doesn't exists and is therefore empty) with the constant Env (which doesn't exists too and is treated as a string. you would get a notice about this with an appropriate error_level)

$tmp = 'News.Env';
$xmlobj->News->$tmp;

或简而言之

$xmlobj->News->{'News.Env'};

更新:如果使用SimpleXML(并按照语法进行操作),则$xmlobjNews-(root-)Element开始.

Update: If you use SimpleXML (and according the syntax you do it) it $xmlobj "starts" with the News-(root-)Element.

$xmlobj->{'News.Env'};

这篇关于php simplexml,xml中的元素中带有点字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:48