If you wish to generate a text representation of a DOMIT_Document or DOMIT_Node -- and all nodes in the hierarchy below -- two methods are available:
toString
toString prints out an unformatted string representation. No whitepace or newlines are added to enhance readability:
echo $cdCollection->toString();
returns:
<cdlibrary><cd id="0001">
<name>Robbie Fulks</name>
<title>Couples in Trouble</title></cd>
<cd id="0002"><name>Richard Thompson</name>
<title>Mock Tudor</title></cd><cd id="0003">
<name>Keller Williams</name>
<title>Laugh</title></cd></cdlibrary>
toNormalizedString
toNormalizedString prints out a formatted string representation, with whitepace inserted to enhance readability:
echo $cdCollection->toNormalizedString();
returns:
<cdlibrary>
<cd id="0001">
<name>Robbie Fulks</name>
<title>Couples in Trouble</title>
</cd>
<cd id="0002">
<name>Richard Thompson</name>
<title>Mock Tudor</title>
</cd>
<cd id="0003">
<name>Keller Williams</name>
<title>Laugh</title>
</cd>
</cdlibrary>
Note that if you pass a value of true to either toString or
toNormalizedString, an html formatted representation of the string will be returned,
suitable for display in the browser:
echo $cdCollection->toNormalizedString(true);
A second boolean parameter can also be passed to both toString and
toNormalizedString. If set to true, any XML entities that exist in the document--
such as ampersands (&) and greater-than signs (>) -- will be replaced by their equivalent character representations.
This will ensure that your XML does not become malformed.
$validXML = $cdCollection->toNormalizedString(true, true);
toNormalizedString requires the file xml_domit_utilities.php to be present in the same directory as xml_domit_parser.php.
expandEmptyElementTags
An empty element is an element that has no children. The XML specification allows for two ways of notating an empty element:
- a compact form, for example <someElement/>
- an expanded form, for example <someElement></someElement>
By default, when DOMIT! uses the toString and
toNormalizedString methods, it uses the compact form of representing an empty tag. However,
some circumstances (in particular working with XHTML code, where browser parsers sometimes choke
on the compact form) may require you use the expanded form.
The method expandEmptyElementTags lets you specify that DOMIT! is to use
the expanded form. For example:
$xmldoc =& new DOMIT_Document();
$xmldoc->expandEmptyElementTags(true);
As of DOMIT! 0.96, the expandEmptyElementTags method can be passed a second parameter --
an array of exceptions to the expansion rule. For instance, if you would like all empty tags except "br" and "img" to be expanded, then you can do the following:
$xmldoc =& new DOMIT_Document();
$xmldoc->expandEmptyElementTags(true, array('br','img'));
If the expansion rule is to collapse all empty tags, then all tags in the exception array will be expanded.
|