Jump to content

Class Design for a sync between incompatible DB and XML


ignace

Recommended Posts

I need some help in regard to class design and sync between DB and XML. The XML looks like:

 

<data>
  <offices>
    <office>*
  <projects>
    <project>*
  <property>
    <property>+
  <employees>
    <employee>*
</data>

Legend: The tags marked with * means they can occur 0 or more times. Those with +, 1 or more times.

 

The XML is deeply nested (there is no 1-to-1 mapping with the DB).

 

Each of these tags have an <id> tag and "foreign keys" for example a property tag can have an office_id tag and a project_id tag.

 

I need to create a class that reads that XML and syncs it with the properties, offices, and projects in the database. It should also be possible to sync it from the database back to the XML. My frst idea went like this:

 

class FatXml
{
    public function findProperty($id) {}
    public function findOffice($id) {} 
    public function findProject($id) {}

    public function addProperty(Property $p) {}
    public function addOffice(Office $p) {} 
    public function addProject(Project $p) {} 

    public function removeProperty(Property $p) {}
    public function removeOffice(Office $p) {}
    public function removeProject(Project $p) {}
}

 

As you can imagine this leads to a lot of code in the FatXml class, recently a new tag was added (employees) which means even MORE lines of code. The mapping alone from leafs (XPath) to setters is hundreds of lines. Does anyone have experience with this sort of thing? Any pointers?

 

Another idea I have been playing with is to let the FatXml object be a facade which encapsulates several smaller "parsers":

 

class FatXml
{
    public function __construct() {
        $this->list['properties'] = new PropertyParser($this);
        $this->list['offices'] = new OfficeParser($this);
        $this->list['projects'] = new ProjectParser($this);
    }
  
    public function findProperty($id) {}    
    public function findOffice($id) {}
    public function findProject($id) {}

    public function addProperty(Property $p) {}
    public function addOffice(Office $p) {}
    public function addProject(Project $p) {}

    public function removeProperty(Property $p) {}
    public function removeOffice(Office $p) {}
    public function removeProject(Project $p) {}
    
    public function parseAll() {
        foreach ($this->list as $parser) $parser->parse();
    }
}

 

Which is I believe better because when a new tag is added, I only need to create a new class, and add it to the list: addParser(ParserInterface).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.