编写xml文件时进行格式化

时间:2022-02-06 21:48:46
  $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 ); 

outputs to a file and looks like

输出到一个文件,看起来像

<RootElement>






<Lead><Contact FirstName="test " LastName="test" Email="testk@gmail.com" StreetAddress="n/a" City="test"/><Qualifications/><PropertyInterest BuilderName="test"/></Lead></RootElement>

there's tons of whitespace... im submitting this to a CRM and i'm assuming that whitespace does make a difference in how it's parsed.

有大量的空白……我将它提交给客户关系管理系统,我假设空格确实会影响解析的方式。

So, how do I format the xml using the DOMDocument?

那么,如何使用DOMDocument格式化xml呢?

1 个解决方案

#1


0  

if that lib is anything like jdom you should be able to set the format on the outputter. something along theses lines:

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

out.setFormat(Format.getPrettyFormat());

#1


0  

if that lib is anything like jdom you should be able to set the format on the outputter. something along theses lines:

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

out.setFormat(Format.getPrettyFormat());