Create XML, with space between nodes

Hello Everyone

I am creating an xml file, and everything works fine. But when i open the file, there are some spaces between nodes

Example

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 1 PA SPA 2020-05-29T17:05:00

As you see after myxml node, the is a space before the header node and after

How did i remove all spaces??

This is the code i use

C_TEXT(vRootRef;$xml_Ref)

$xml_node:=“myxml/header/”

vRootRef:=DOM Create XML Ref(“myxml”)
$xml_Ref:=DOM Create XML element(vRootRef;$xml_node)

$xml_Ref_node:=DOM Create XML element($xml_Ref;“recordCount”)
DOM SET XML ELEMENT VALUE($xml_Ref_node;$recordCount)
$xml_Ref_node:=DOM Create XML element($xml_Ref;“countryCode”)
DOM SET XML ELEMENT VALUE($xml_Ref_node;$countryCode)
$xml_Ref_node:=DOM Create XML element($xml_Ref;“partnerCode”)
DOM SET XML ELEMENT VALUE($xml_Ref_node;$partnerCode)
$xml_Ref_node:=DOM Create XML element($xml_Ref;“createDate”)
DOM SET XML ELEMENT VALUE($xml_Ref_node;$createDate)

DOM EXPORT TO FILE(vRootRef;“filedata.xml”)
DOM CLOSE XML(vRootRef)

Hi Abdiel,

When I paste your code into a 4D Method, I also see the space.

I looked closer and noticed that the double quotation marks in your code appear to be ‘smart quotes’ rather than ASCII 34. I don’t know where those were introduced, but if you replace them with ASCII 34 the code seems to work.

HTH,

Tom Benedict

Sometimes, it might make more sense to use PROCESS 4D TAGS to generate XML. With templates, you have more control over pretty printing.

In your case, XML indentation of XML SET OPTIONS may suffice.

For existing documents, you could try

or

http://xmlsoft.org/xmllint.html

There are some ambiguities as to what qualifies as cosmetic, non-essential spaces in XML, so there is no standard way to remove whitespaces.

c.f.

http://xmlsoft.org/html/libxml-parser.html#xmlKeepBlanksDefault

Hi,
I tried (with code reworked slightly) and I get this


C_TEXT($vt_xmlxpath)
$vt_xmlxpath:="/myxml/header"
  //$vt_xmlxpath:="myxml/header/"

C_TEXT($vt_rootRef;$vt_elementRef)
$vt_rootRef:=DOM Create XML Ref("myxml")
If (ok=1)
	$vt_elementRef:=DOM Create XML element($vt_rootRef;$vt_xmlxpath)
	
	$vt_elementRef_node:=DOM Create XML element($vt_elementRef;"recordCount")
	DOM SET XML ELEMENT VALUE($vt_elementRef_node;1)
	
	$vt_elementRef_node:=DOM Create XML element($vt_elementRef;"countryCode")
	DOM SET XML ELEMENT VALUE($vt_elementRef_node;"PA")
	
	$vt_elementRef_node:=DOM Create XML element($vt_elementRef;"partnerCode")
	DOM SET XML ELEMENT VALUE($vt_elementRef_node;"SPA")
	
	$vt_elementRef_node:=DOM Create XML element($vt_elementRef;"createDate")
	DOM SET XML ELEMENT VALUE($vt_elementRef_node;"2020-05-29T17:05:00")
	
	DOM EXPORT TO VAR($vt_rootRef;$vt_xml)
	SET TEXT TO PASTEBOARD($vt_xml)
	
	DOM CLOSE XML($vt_rootRef)
End if 

  //   <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
  //   <myxml>
  //   
  //     <header>
  //       <recordCount>1</recordCount>
  //       <countryCode>PA</countryCode>
  //       <partnerCode>SPA</partnerCode>
  //       <createDate>2020-05-29T17:05:00</createDate>
  //     </header>
  //   
  //   </myxml>

I solve this way

C_TEXT($xmltext)
$xmltext:=Document to text(“filedata.xml”)
$xmltext:=Replace string($xmltext;"\r\n\r\n";"\r\n")
TEXT TO DOCUMENT(“filedata.xml”)

Hi,

This is not recommended in general… The “\r\n\r\n” could be in CDATA for instance (and then it is not element indentation) and in that case it should not be replaced…

As Miyako said, this will make an xml without any indentation (call it after creation the DOM root ref.

XML SET OPTIONS ( $vt_rootRef ; XML indentation ; XML no indentation) 

Indentation is here to help human read an xml. There are tools to indent an xml without indentation.

HTH

1 Like