Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Assume we have the following XML document:   

...

Code Block
languagexml
 <?xml version="1.0" encoding="UTF-8"?>

...

 <root>

...

         <organization type="governmental">

...

                 <name>Company A</name>

...

                 <employees>

...

                         <employee>

...

                                 <name>John Doe</name>

...

                                 <gender>M</gender>

...

                         </employee>

...

                         <employee>

...

                                 <name>Jane Doe</name>

...

                                 <gender>F</gender>

...

                         </employee>

...

                 </employees>

...

         </organization>

...

 

...

         <organization type="company">

...

                 <name>Company B</name>

...

                 <employees>

...

                         <employee>

...

                                 <name>Peter</name>

...

                                 <gender>M</gender>

...

                         </employee>

...

                         <employee>

...

                                 <name>Bob</name>

...

                                 <gender>M</gender>

...

                         </employee>

...

                 </employees>

...

         </organization>

...

 </root>

Now imagine that you want to have a table of employee names and gender information, and another table with company name and type information. We define our DataContext and those tables like this:  

Code Block
languagejava
XmlSaxTableDef employeeTableDef = new XmlSaxTableDef(
   "/root/organization/employees/employee",
   new String[] {
     "/root/organization/employees/employee/name",
     "/root/organization/employees/employee/gender"
   }
);
 
XmlSaxTableDef organizationTableDef = new XmlSaxTableDef(
   "/root/organization",
   new String[] {
     "/root/organization/name",
     "/root/organization@type"
   }
);
 
DataContext dc = new XmlSaxDataContext(new File("my_file.xml"), employeeTableDef, organizationTableDef);

...

 

row_id

/name

/gender

index(/root/organization)

0

John Doe

M

0

1

Jane Doe

F

0

2

Peter

M

1

3

Bob

M

1

Moving on, you will be able to define both joins and lookups using this foreign key. For example:

Code Block
languagejava
  Column fk = employeeTable.getColumnByName("index(/root/organization)");


  Column empName = employeeTable.getColumnByName("/name");
  Column orgId = organizationTable.getColumnByName("row_id");
  Column orgName = organizationTable.getColumnByName("/name");
 
  Query q = dc.query().from(employeeTable)
   .innerJoin(organizationTable).on(fk, orgId)
   .select(empName).as("employee")
   .select(orgName).as("company").toQuery();
  DataSet ds = dc.executeQuery(q);

The contents of this queried dataset will now be:  

 

employee

company

John Doe

Company A

Jane Doe

Company A

Peter

Company B

Bob

Company B