Action executed in 0.000

XmlDoc Substring

I wrote a useful PHP class i'd like to share. The whole purpose to to take a substring of an XML document while preserving well-formedness. I created this function because i incorporate other people's articles into my blog, but need to limit the size of summaries. Examples and source code are included below.

I wanted to create a class "myXmlDoc" as an extension of xmldoc in PHP, but apparently xmldoc is not a class, just a collection of functions.

This is how xmlDoc is used.

$str_Doc = "<person><name>Brian</name><hobby>writing code</hobby>";
$XmlDoc = new xmlDoc( $str_Doc );
if ($XmlDoc->isWellFormed())
   $SS = $XmlDoc->substring( 30 );

Here's a larger example.

Here's the source code licensed under GPL.

One thing this code doesn't do is maintain validity. Consider that an address element is composed of mandatory street, city, and state elements. This function would cut the state element first, then city, and finally street if you wanted a short enough substring.

Tangentially, i would represent a postal address like i represent time in a hierachial structure, like this.

  <country value='USA'>
    <state value='MD'>
      <city value='Rockville'>
        <zip value='20742'>
          <street value='Ruth Lane'>
            <number value='169' />

Each sub element is optional. This allows you to have arbitrary precision. Using this method, a substring will maintain validity in addition to well-formedness. If you are forced to omit data, the first to go are the finest details. Saying you live in USA has more meaning than saying you live at 169.


none yet

Post a Comment

* indicates a required field
anonymous (If you want to identify yourself, please sign in first.)
required This field is required.

Max size is 2 MB, aspect ratio 3:4 width:height
required This field is required.
Please include a short description.
required This field is required.

480 characters remaining.
is public

(Use this field if you have to. 3000 characters remaining.)
2 nickels, 1 quarter, 1 penny + 100

Trackback URL