写入xml文件时的格式

时间:2011-04-14 22:06:29

标签: php xml domdocument

  $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?

1 个答案:

答案 0 :(得分:0)

如果该lib与jdom类似,您应该能够在输出器上设置格式。沿着这些路线的东西:

out.setFormat(Format.getPrettyFormat());