【发布时间】:2011-08-05 22:20:21
【问题描述】:
$doc = new DOMDocument();
$doc->load('xml/lead.xml');
$doc->formatOutput = true;
$tags = $doc->getElementsByTagName('RootElement');
$r = $tags->item(0);
$a = $doc->createElement( 'Lead' );
$r->appendChild( $a );
$b = $doc->createElement( 'Contact' );
$b->setAttribute('FirstName', ''.$name.'');
$b->setAttribute('LastName', ''.$last_name.'');
$b->setAttribute('Email', ''.$email.'');
$b->setAttribute('StreetAddress', ''.$address.'');
$b->setAttribute('City', ''.$city.'');
$a->appendChild( $b );
$c = $doc->createElement( 'Qualifications' );
$a->appendChild( $c );
$d = $doc->createElement( 'PropertyInterest' );
$d->setAttribute("BuilderName","Test");
$a->appendChild( $d );
输出到一个文件,看起来像
<RootElement>
<Lead><Contact FirstName="test " LastName="test" Email="testk@gmail.com" StreetAddress="n/a" City="test"/><Qualifications/><PropertyInterest BuilderName="test"/></Lead></RootElement>
有大量的空白...我将其提交给 CRM 并且我假设空白确实会影响它的解析方式。
那么,如何使用 DOMDocument 格式化 xml?
【问题讨论】:
-
你如何转储/打印生成的输出?
-
实际上,如果它是正确的 XML 解析器,它将忽略空格。您也可以尝试从头开始编写 XML 文件,在我看来,您正在加载的文件中包含空格。
-
您可以尝试将其输出为字符串,然后使用 Tidy 进行解析。
-
format xml string 的可能重复项
标签: php xml domdocument