PHP Classes

LOM: Extract information from XML documents

Recommend this page to a friend!
  Info   View files Example   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 79 All time: 10,097 This week: 455Up
Version License PHP version Categories
lom 0.2Freeware5XML, PHP 5, Parsers
Description 

Author

This package can extract information from XML documents.

It can parse a given XML file and perform queries to extract data from certain tags.

The query expressions syntax allow to specify values to match tag attribute names.

Picture of Jill Lingoff
Name: Jill Lingoff <contact>
Classes: 3 packages by
Country: France France
Age: ???
All time rank: 3951101 in France France
Week rank: 360 Up12 in France France Up
Innovation award
Innovation award
Nominee: 1x

Example

<?php

include('O.php');
$O = new O('test.xml');
print(
'_ is a special character in the query string so searching for a tagname with an underscore in it will create a conflict (should be 0 matches): ');var_dump($O->_('big_container'));
print(
'to find a tag with an underscore in its name, that part of the query string must be encoded (should be an array with 1 match for the big_container): ');var_dump($O->_($O->enc('big_container')));
print(
'all persons (should be an array of the 5 persons): ');var_dump($O->_('person'));
print(
'using a dot in the query string indicates that you are interested in a tag other than the last tag; all persons named sally (should be an array of the 3 sallys): ');var_dump($O->_('.person_name=sally'));
print(
'lastnames (should be an array of the 3 sallys\' lastnames since they were last mentioned): ');var_dump($O->_('lastname'));
print(
'lastname of a person with a name of zero length (should be 0 matches): ');var_dump($O->_('lastname', '.person_name='));
print(
'__ in the query string instead of _ means offspring instead of child; tag2 with name offspring (should be an array with 1 match for tag2 that has the offspring deep one): ');var_dump($O->_('.tag2__name'));
print(
'* is a special character in the query string meaning any tag; all things with a lastname (should be an array of the 5 persons since we\'re being less specific and so have to go to a broader context): ');var_dump($O->_('.*_lastname'));
print(
'all things with a lastname in the big_container (should be an array of the 5 persons): ');var_dump($O->_('.*_lastname', $O->_($O->enc('big_container'))));
print(
'| in the query string acts as a logical or to put more than one query string together; persons named mike or sally (should be an array with 4 matches): ');var_dump($O->_('.person_name=mike|.person_name=sally'));
print(
'lastname of second sally (should be mott): ');var_dump($O->_('lastname', $O->_('.person_name=sally')[1]));
print(
'third person (should be an array with 1 match for sally supado): ');var_dump($O->_('person[2]', $O->enc('big_container'))); // still may be getting too many results when querying within the second sally from the previous line (would should be getting 0)
print('hobby (should be world domination by contextual querying since third person was last mentioned): ');var_dump($O->_('hobby'));
print(
'@ in the query string refers to an attribute; sallys aged 16 (should be an array of 2 sallys): ');var_dump($O->_('.person@age=16_name=sally'));
print(
'persons with an age attribute but without the attribute value specified (should be an array of 2 sallys and tom blabbo): ');var_dump($O->_('person@age', $O->enc('big_container')));
print(
'hobby of person aged 16 with blue eyes (should be skiing): ');var_dump($O->_('person@age=16@' . $O->enc('eye_color') . '=blue_hobby'));
print(
'lastname (should be array of kellerman and mott and blabbo by contextual querying since these are the persons with lastnames last mentioned): ');var_dump($O->_('lastname'));
print(
'saving person with name sally and lastname mott to a living variable named live_var (should be true): ');var_dump($O->set_variable('live_var', $O->_('.person_name=sally&lastname=mott')));
print(
'hobby of live_var (should be swimming): ');var_dump($O->_('hobby', $O->get_variable('live_var')));
print(
'& in the query string acts as a logical and to select by more than one tag; person with name tom and lastname blabbo (should be an array with 1 match for tom blabbo): ');$person = $O->_('.person_name=tom&lastname=blabbo');var_dump($person);
print(
'person with name sally and lastname blabbo (should be 0 matches): ');var_dump($O->_('.person_name=sally&lastname=blabbo'));
print(
'age of $person variable (should be 999): ');var_dump($O->get_attribute('age', $person));
print(
'hobby of $person variable (should be sleeping): ');var_dump($O->_('hobby', $person));
print(
'setting hobby to waking up (should say waking up): ');var_dump($O->set('hobby', 'waking up'));
print(
'hobby (should be waking up): ');var_dump($O->_('hobby'));
// reset the change after testing
//$O->set('hobby', 'sleeping');
print('setting hobby of person with lastname mott to breathing (should be an array with 1 match for sally mott): ');var_dump($O->set('hobby', 'breathing', '.person_lastname=mott'));
print(
'hobby of person with name sally and lastname kellerman (should be skiing): ');var_dump($O->_('hobby', '.person_name=sally&lastname=kellerman'));
print(
'hobby of live_var (should be breathing): ');var_dump($O->_('hobby', $O->get_variable('live_var')));
print(
'adding a new person (should be an array with 1 match for santa klaus): ');var_dump($O->new_('<person age="33" beard_color="white"><name>santa</name><lastname>klaus</lastname><hobby>making presents</hobby></person>', $O->enc('big_container')));
print(
'deleting a person (should be true): ');var_dump($O->delete('.person_name=santa'));
print(
'selecting using tagname index (should be ' . htmlentities('<a id="8"><b><c>tagvalue3</c></b></a>') . '): ');var_dump($O->get_tagged('.a[8]_b_c'));
print(
'selecting using tagvalue index (should be ' . htmlentities('<a id="5"><b><c att2="attvalue4">tagvalue2</c></b></a>') . '): ');var_dump($O->get_tagged('.a_b_c=tagvalue2[2]'));
print(
'selecting using attributes index (should be ' . htmlentities('<a id="6"><b><c att2="attvalue5">tagvalue3</c></b></a>') . '): ');var_dump($O->get_tagged('.a_b_c@att2[1]', $O->enc('big_container')));
print(
'selecting using an attributes index that is too high (should be 0 matches): ');var_dump($O->get_tagged('.a_b_c@att1=attvalue1[6]'));
//$O->save_LOM_to_file('test.xml');
$O->dump_total_time_taken();

?>


Details

LOM

Version 0.2

If you are manipulating XML with PHP then this is the code you want. LOM used to use array-based internal data structure but now uses string-based internal data structure. With this type of optimization it's about 10 times faster. Another improvement is that the external data structure (that which is accesible as the results of queries) is now the more familiar (to users of PHP regular expressions) string-offset pairs.

See test.php for usage examples.

To see an example of what's possible with LOM, check out a game made using it:

http://flaurora-sonora.000webhostapp.com/infini/bbalof.php?scenario=0

Version 0.1

LOM is an XML querying language; or slang, if you prefer. In terms of other querying languages: this one would be said to use a dynamic, rather than static, context. So query results depend on code-wise previous query results. Basically, it allows a coder to write code more lazily by having LOM assume that something not very specifically referenced should be looked for within the most relevant contexts (usually the most recent ones). This pushes it a little towards being conversational rather than only logical. An example will probably clarify things:

Sarah: I want lots of friends. Do you have many friends?<br> Jill: I have some but my brother has more.<br> Sarah: Oh yeah, my brother has lots of friends too.<br> Jill: What are their names?<br>

Based on the above conversation, we can probably see that what we are interested in would be the names of Sarah's brother's friends and not the names of Jill's friends or the names of all the friends Sarah and Jill know or the names of everything in the universe. LOM makes the syntax for this query simple; it would be $O->_('name'); assuming that the rest of the conversation were similarly coded.

See test.php for usage examples.


  Files folder image Files  
File Role Description
Plain text file O.php Class Class source
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file test.php Example Example script
Accessible without login Plain text file test.xml Data Auxiliary data

 Version Control Reuses Unique User Downloads Download Rankings  
 100%1
Total:79
This week:0
All time:10,097
This week:455Up