简体   繁体   English

在XMLwriter PHP中定义变量或常量

[英]Defining Variables or constant in XMLwriter PHP

i am new in PHP. 我是PHP新手。 Using xmlwriter to produce an xml file. 使用xmlwriter生成xml文件。 I have below code. 我有下面的代码。

$xml = new XMLWriter(); 
$xml->openURI('test.xml');

$xml->setIndent(true);
$mmmid = '1981';


$xml->startDocument('1.0', 'UTF-8');
$xml->text('<raml xmlns="raml21.xsd" version="2.1">');
$xml->text('    <cmData scope="all" type="plan">'); 
$xml->text('        <header>');

$xml->text('            <log dateTime="2012-11-05T12:27:50+02:00" appInfo="Manager" user="WebUI" appVersion="4.5133.90" action="created"/>');

$xml->text('        </header>');    

$xml->text('                    <managedObject class="MRBTS" distName="IDENTITY-'$mmmid'" operation="create" version="1803">');
$xml->text('                    <p name="btsName">B1123</p>');
$xml->text('                </managedObject>');

I want to define the mmmid in the output of xml file, so it will be appeared in the output distName="IDENTITY-1981" 我想在xml文件的输出中定义mmmid,因此它将出现在输出distName =“ IDENTITY-1981”中

How can i define variable mmmid in the code and expected printed in output of the text above? 如何在代码中定义变量mmmid并在上面的文本输出中预期打印?

Thank you a lot, Br,FM 非常感谢Br,FM

That would be it: 就是这样:

$xml->text('                    <managedObject class="MRBTS" distName="IDENTITY-' . $mmmid . '" operation="create" version="1803">');

You concatinate strings in PHP via the . 您可以通过来在PHP中添加字符串. operator, see the PHP manual on strings 操作符,请参见有关字符串的PHP手册

When concatenating a variable with strings in PHP there are several ways of doing it. 在PHP中将变量与字符串连接时,有几种方法可以实现。

Use double quotes " as surrounding delimiters 使用双引号"作为分隔符

When using double quotes " in PHP you can use variables inside the string as usual 在PHP中使用双引号" ,您可以照常在字符串内使用变量

$foo = 4;
echo "Hello $foo";

Output : 输出

Hello 4

This would give the following solution for your problem: 这将为您的问题提供以下解决方案:

# Note that you need to escape the original double quotes with a backslash
$xml->text("<managedObject distName=\"IDENTITY-$mmmid\" operation=\"create\" version=\"1803\">');

Concatenate with a dot . 与一个点连接.

Another way of binding a variable with text is by using a dot . 将变量与文本绑定的另一种方法是使用点. between the variable and string (this also works with only strings or only variables as well). 在变量和字符串之间(这也仅适用于字符串或仅适用于变量)。 For this method you can use either single ' or double quotes " . 对于这种方法,你可以使用单'或双引号"

$foo = 4;
echo 'Hello ' . $foo;

Output 产量

Hello 4

And with this method it would give you this 使用这种方法,它将为您提供

$xml->text('<managedObject distName="IDENTITY-' . $mmmid . '" operation="create" version="1803">');

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

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