XML::Tidy

XML::Tidy is a tidy indenting of XML documents.
Download

XML::Tidy Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Pip Stuart
  • Publisher web site:
  • http://search.cpan.org/~pip/XML-Tidy-1.2.54HJnFa/Tidy.pm

XML::Tidy Tags


XML::Tidy Description

XML::Tidy is a tidy indenting of XML documents. XML::Tidy is a tidy indenting of XML documents.SYNOPSIS use XML::Tidy; # create new XML::Tidy object from MainFile.xml my $tidy_obj = XML::Tidy->new('filename' => 'MainFile.xml'); # Tidy up the indenting $tidy_obj->tidy(); # Write out changes back to MainFile.xml $tidy_obj->write();USAGEnew()This is the standard Tidy object constructor. It can take the same parameters as an XML::XPath object constructor to initialize the XML document object. These can be any one of: 'filename' => 'SomeFile.xml' 'xml' => $variable_which_holds_a_bunch_of_XML_data 'ioref' => $file_InputOutput_reference 'context' => $existing_node_at_specified_context_to_become_new_objreload()The reload() member function causes the latest data contained in a Tidy object to be re-parsed which re-indexes all nodes. This can be necessary after modifications have been made to nodes which impact the tree node hierarchy because XML::XPath's find() member preserves state info which can get out-of-sync. reload() is probably rarely useful by itself but it is needed by strip() && prune() so it is exposed as a method in case it comes in handy for other uses.strip()The strip() member function searches the Tidy object for all mixed-content (ie. non-data) text nodes && empties them out. This will basically unformat any markup indenting. strip() is used by compress() && tidy() but it is exposed because it could be worthwhile by itself.tidy()The tidy() member function can take a single optional parameter as the string that should be inserted for each indent level. Some examples: # Tidy up indenting with default two (2) spaces per indent level $tidy_obj->tidy(); # Tidy up indenting with four (4) spaces per indent level $tidy_obj->tidy(' '); # Tidy up indenting with one (1) tab per indent level $tidy_obj->tidy("t");The default behavior is to use two (2) spaces for each indent level. The Tidy object gets all mixed-content (ie. non-data) text nodes reformatted to appropriate indent levels according to tree nesting depth.NOTE: There seems to be a bug in XML::XPath which does not allow finding XML processing instructions (PIs) properly so they have been commented out of tidy(). This means that tidy() unfortunately removes processing instructions from files it operates on. I hope this shortcoming can be repaired in the near future. tidy() also disturbs some XML escapes in whatever ways XML::XPath does. It has also been brought to my attention that these modules also strip CDATA tags from XML files / data they operate on. Even though CDATA tags don't seem very common, I wish they were easy to support. Hopefully the vast majority of files will work fine.compress()The compress() member function calls strip() on the Tidy object then creates an encoded comment which contains the names of elements && attributes as they occurred in the original document. Their respective element && attribute names are replaced with just the appropriate index throughout the document.compress() can accept a parameter describing which node types to attempt to shrink down as abbreviations. This parameter should be a string of just the first letters of each node type you wish to include as in the following mapping: e = elements a = attribute keys v = attribute values *EXPERIMENTAL* t = text nodes *EXPERIMENTAL* c = comment nodes *EXPERIMENTAL* n = namespace nodes *not-yet-implemented*Attribute values ('v') && text nodes ('t') both seem to work fine with current tokenization. I've still labeled them EXPERIMENTAL because they seem more likely to cause problems than valid element or attribute key names. I have some bugs in the comment node compression which I haven't been able to find yet so that one should be avoided for now. Since these three node types ('vtc') all require tokenization, they are not included in default compression ('ea'). An example call which includes values && text would be: $tidy_obj->compress('eatv');The original document structure (ie. node hierarchy) is preserved. compress() significantly reduces the file size of most XML documents for when size matters more than immediate human readability. expand() performs the opposite conversion.expand()The expand() member function reads any XML::Tidy::compress comments from the Tidy object && uses them to reconstruct the document that was passed to compress().bcompress('BinaryOutputFilename.xtb')The bcompress() member function stores a binary representation of any Tidy object. The format consists of: 0) a null-terminated version string 1) a byte specifying how many bytes later indices will be 2) the number of bytes from 1 above to designate the total string count 3) the number of null-terminated strings from 2 above 4) the number of bytes from 1 above to designate the total integer count 5) the number of 4-byte integers from 4 above 6) the number of bytes from 1 above to designate the total float count 7) the number of double-precision floats from 6 above 8) node index sets until the end of the fileNormal node index sets consist of two values. The first is an index into the three lists (with the number of bytes from 1) as if they were all linear. The second is a single-byte integer identifying the node type (using standard DOM node enumerations).A few special cases exist in node index sets though. If the index is null, it is interpreted as a close-element tag (so no accompanying type value is read). On the other end, when the index is non-zero, the type value is always read. In the event that the type corresponds to an attribute or a processing-instruction, the next index is read (without another accompanying type value) in order to complete the data fields required by those node types.bexpand('BinaryInputFilename.xtb')The bexpand() member function reads a binary file which was previously written from bcompress(). bexpand() is an XML::Tidy object constructor like new().prune()The prune() member function takes an XPath location to remove (along with all attributes && child nodes) from the Tidy object. For example, to remove all comments: $tidy_obj->prune('//comment()');or to remove the third baz (XPath indexing is 1-based): $tidy_obj->prune('/foo/bar/baz');Pruning your XML tree is a form of tidying too so it snuck in here. =) It seems XML::XPath objects are dramatically more useful when they all have access to this class of additional member functions.write()The write() member function can take an optional filename parameter to write out any changes to the Tidy object. If no parameters are given, write() overwrites the original XML document file (if a 'filename' parameter was given to the constructor).write() will croak() if no filename can be found to write to.write() can also take a secondary parameter which specifies an XPath location to be written out as the new root element instead of the Tidy object's root. Only the first matching element is written.toString()The toString() member function is almost identical to write() except that it takes no parameters && simply returns the equivalent XML string as a scalar. It is a little weird because normally only XML::XPath::Node objects have a toString member but I figure it makes sense to extend the same syntax to the parent object as well since it is a useful option.Requirements:· PerlWhat's New in This Release:· added support for binary ints && floats in bcompress()· tightened up binary format && added pod


XML::Tidy Related Software