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).

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.