Home
Navigating the SDO data graph
XPath expressions are used to obtain data from the data objects present in the data graph after the XML file is loaded.
Figure | -23 shows the data graph for the accounts.xml file.
![]()
Figure 10-23 Accounts data graph
![]()
Add the code shown in Example | -8 to the main method. Example 10-8 Java code for navigating the SDO data graph
// navigating the SDO data graphAccountsType accountsType = documentRoot.getAccounts();DataObject accountsTypeImpl = (AccountsTypeImpl) accountsType;DataObject account1 = accountsTypeImpl.getDataObject("account.0");System.out.println("\n\nThe first account is: " + account1 + "\n");DataObject account2 = accountsTypeImpl.getDataObject("account[accountID = '123457']");System.out.println("The second account is: " + account2 + "\n");DataObject account2CustomerInfo = accountsTypeImpl.getDataObject("account[accountID = '123457']/customerInfo");System.out.println("The second account customer information is: " +account2CustomerInfo + "\n");String account1CustomerName = account1.getString("customerInfo/firstName");System.out.println("The first account customer first name is " +account1CustomerName + "\n");
![]()
![]()
The XPath dot notation is used to index data objects. The first object has index 0, therefore account.0 returns the first account data object.
![]()
The XPath expression account[accountID = '123457'] returns the account data object whose account ID equals 123457.
![]()
account[accountID = '123457']/customerInfo is an XPath expression that returns a data object multiple levels below the root data object.
![]()
Right-click SDOSample and select Run As Æ Java Application. You can also select Run Æ Run History Æ SDOSample. The Console output from the SDO graph navigation code is shown here:
The first account is: com.xml.rad75.itso.impl.AccountImpl@d8d0d8d (accountID: 123456, accountType: Savings, balance: 20000, interest: 3.5)
The second account is: com.xml.rad75.itso.impl.AccountImpl@64d764d7 (accountID: 123457, accountType: Fixed, balance: 50000, interest: 6)
The second account customer information is: com.xml.rad75.itso.impl.CustomerInfoTypeImpl@65506550 (firstName: Ueli, lastName: Wahli, phoneNumber: (408) 345-6780)
The first account customer first name is Brian
ibm.com/redbooks