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

Adam
  • 8,849
  • 16
  • 67
  • 131
  • How are you dumping/printing the generated output? – Marc B Apr 14 '11 at 22:10
  • 1
    actually if it's a proper XML parser it will ignore whitespace. You could also try writing the XML file from scratch, it seems to me the file you're loading has the whitespace in it. – Khez Apr 14 '11 at 22:10
  • You could try outputting it to a string, and then parse it with Tidy. – Blender Apr 14 '11 at 22:16
  • 1
    possible duplicate of [format xml string](http://stackoverflow.com/questions/3616540/format-xml-string/3616722#3616722) – Gordon Apr 14 '11 at 22:17
  • 1
    also see [`DOMDocument::preserveWhiteSpace`](http://de.php.net/manual/en/class.domdocument.php#domdocument.props.preservewhitespace) – Gordon Apr 14 '11 at 22:18
  • The comments above are all correct, but after looking at your code I'm confused. You're reading an XML document then outputting to the same file? – FilmJ Apr 14 '11 at 23:24

1 Answers1

0

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

out.setFormat(Format.getPrettyFormat());
Todoy
  • 1,146
  • 1
  • 9
  • 17