|
Writing XML Using View Objects
BC4J view objects feature built-in support for reading and writing XML. The API's to do this are very straightforward, readXML() and writeXML() respectively. This quick article explains the WriteXMLExample.zip project which you can download.
The project contains a simple test.TestModule application module containing instances of view objects test.DeptView and test.EmpView which are view-linked.
The test.TestClient example program looks like this:
package test;
import oracle.jbo.ApplicationModule;
import oracle.jbo.ViewObject;
import oracle.jbo.XMLInterface;
import oracle.jbo.client.Configuration;
import org.w3c.dom.Node;
import java.io.PrintWriter;
import com.sun.java.util.collections.HashMap;
import oracle.xml.parser.v2.XMLNode;
public class TestClient {
private static final String AMDEF = "test.TestModule";
private static final String CONFIG = "TestModuleLocal";
private static final String DEPTVODEF = "test.DeptView";
private static final String EMPVODEF = "test.EmpView";
private static final String[] DEPTATTRS = {"Deptno","Dname","Employees"};
private static final String[] EMPATTRS = {"Empno","Ename","Sal"};
public static void main(String[] args) throws Throwable {
ApplicationModule am = Configuration.createRootApplicationModule(AMDEF,CONFIG);
ViewObject vo = am.findViewObject("Departments");
vo.executeQuery();
HashMap attrMap = new HashMap(2);
attrMap.put(DEPTVODEF,DEPTATTRS);
attrMap.put(EMPVODEF,EMPATTRS);
Node n = vo.writeXML(XMLInterface.XML_OPT_ALL_ROWS,attrMap);
// Make this PrintWriter wrap a FileWriter to print the
// XML out to a file instead
PrintWriter pw = new PrintWriter(System.out);
((XMLNode)n).print(pw);
Configuration.releaseRootApplicationModule(am,true);
}
}
Running the program against a standard SCOTT schema produces results like the following:
<Departments>
<Department>
<Deptno>10</Deptno>
<Dname>ACCOUNTING</Dname>
<Employees>
<Employee>
<Empno>7782</Empno>
<Ename>CLARK</Ename>
<Sal>2450</Sal>
</Employee>
<Employee>
<Empno>7839</Empno>
<Ename>KING</Ename>
<Sal>5000</Sal>
</Employee>
<Employee>
<Empno>7934</Empno>
<Ename>MILLER</Ename>
<Sal>1300</Sal>
</Employee>
</Employees>
</Department>
<!-- etc. -->
<Department>
<Deptno>40</Deptno>
<Dname>OPERATIONS</Dname>
</Department>
</Departments>
A few interesting things to note about the example are:
EmpView and DeptView view objects both have custom properties XML_ROW_ELEMENT and XML_ELEMENT defined that customize the default element names for department.
- I edited the view link properties of
WorksInDeptLink in the view link editor to customize the name of the view link accessor attributes.This way, to access the related collection of employees for the current department row, I can access the "Employees" attribute. To access the department for a current employee row, I can access its "Department" attribute.
- I'm using the
writeXML() API overload that takes a HashMap to have precise control over which attributes get rendered to XML at every level.By including the name of the view link accessor attribute "Employees" in the list of attribute names related to the "test.DeptView" view object definition, I indicate to BC4J to include that attribute and effectively "traverse down" into those details. Leaving it out of the list would have been an indication that I did not want those details to appear in the XML result.
- The
HashMap in use here is com.sun.java.util.collections.HashMap from ./BC4J/lib/collections.jar and not the java.util.HashMap. This is for historical reasons when BC4J still required JDK 1.1.8 compatibility.
|