PageRenderTime 57ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/basex-examples/src/main/java/org/basex/examples/xqj/cfoster/Part4.java

https://github.com/JensErat/basex
Java | 204 lines | 114 code | 50 blank | 40 comment | 6 complexity | dd32570d02de93ae27d0c73387efe087 MD5 | raw file
  1. package org.basex.examples.xqj.cfoster;
  2. import java.io.*;
  3. import java.util.*;
  4. import javax.xml.datatype.*;
  5. import javax.xml.namespace.*;
  6. import javax.xml.parsers.*;
  7. import javax.xml.xquery.*;
  8. import org.w3c.dom.*;
  9. /**
  10. * XQJ Example, derived from the XQJ Tutorial
  11. * <a href="http://www.cfoster.net/articles/xqj-tutorial">
  12. * http://www.cfoster.net/articles/xqj-tutorial</a> from Charles Foster.
  13. *
  14. * Part 4: XDM Model within XQJ.
  15. *
  16. * @author BaseX Team 2005-16, BSD License
  17. */
  18. public final class Part4 extends Main {
  19. /**
  20. * Main method of the example class.
  21. * @param args (ignored) command-line arguments
  22. * @throws Exception exception
  23. */
  24. public static void main(final String[] args) throws Exception {
  25. init("4: XDM Model within XQJ");
  26. // Create the connection
  27. XQConnection conn = connect();
  28. // Create XQuery items from int values
  29. info("Create XQuery items from int values");
  30. XQItem[] items = new XQItem[7];
  31. // Create an XQItem type, with a type of xs:int and a value of 0
  32. items[0] = conn.createItemFromInt(0, null);
  33. XQItemType xsInteger = conn.createAtomicType(XQItemType.XQBASETYPE_INTEGER);
  34. XQItemType xsString = conn.createAtomicType(XQItemType.XQBASETYPE_STRING);
  35. XQItemType xsByte = conn.createAtomicType(XQItemType.XQBASETYPE_BYTE);
  36. XQItemType xsDecimal = conn.createAtomicType(XQItemType.XQBASETYPE_DECIMAL);
  37. XQItemType xsLong = conn.createAtomicType(XQItemType.XQBASETYPE_LONG);
  38. // Create an XQItem, with a type of xs:integer and a value of 1
  39. items[1] = conn.createItemFromInt(1, xsInteger);
  40. // Create an XQItem, with a type of xs:string and a value of 2
  41. items[2] = conn.createItemFromInt(2, xsString);
  42. // Create an XQItem, with a type of xs:byte and a value of 3
  43. items[3] = conn.createItemFromInt(3, xsByte);
  44. // Create an XQItem, with a type of xs:decimal and a value of 4
  45. items[4] = conn.createItemFromInt(4, xsDecimal);
  46. // Create an XQItem, with a type of xs:long and a value of 5
  47. items[5] = conn.createItemFromInt(5, xsLong);
  48. // Try to create an XQItem, with a type of xs:byte and a value of 1000
  49. // This causes an XQException, because the
  50. // value 1000 is outside the range of xs:byte (-128 to 127)
  51. try {
  52. items[6] = conn.createItemFromInt(1000, xsByte);
  53. } catch(final XQException ex) {
  54. System.out.println(ex.getMessage());
  55. }
  56. for(XQItem it : items) {
  57. if(it != null) it.writeItem(System.out, null);
  58. System.out.print(' ');
  59. }
  60. System.out.println();
  61. // Create items from atomic values
  62. info("Create items from atomic values");
  63. XQItemType date = // xs:date
  64. conn.createAtomicType(XQItemType.XQBASETYPE_DATE);
  65. XQItemType hex = // xs:hexBinary
  66. conn.createAtomicType(XQItemType.XQBASETYPE_HEXBINARY);
  67. XQItem dateValue = conn.createItemFromAtomicValue("2007-01-23", date);
  68. XQItem binaryData = conn.createItemFromAtomicValue("48656C6C6F", hex);
  69. dateValue.writeItem(System.out, null);
  70. System.out.println();
  71. binaryData.writeItem(System.out, null);
  72. System.out.println();
  73. // Create items from Java objects
  74. info("Create items from atomic values");
  75. items = new XQItem[3];
  76. // Create an XQItem with a type of xs:int
  77. items[0] = conn.createItemFromObject(5, null);
  78. // Create an XQItem with a type of xs:float
  79. items[1] = conn.createItemFromObject(123.4f, null);
  80. // Create an XQItem with a type of xs:hexBinary
  81. items[2] = conn.createItemFromObject(new byte[] { 1, 2, 3, 4 }, null);
  82. for(XQItem it : items) {
  83. if(it != null) System.out.println(it.getAtomicValue());
  84. }
  85. // Create and bind XQuery sequences
  86. info("Create and bind XQuery sequences");
  87. List<Object> list = new ArrayList<>();
  88. list.add(conn.createItemFromInt(1, null));
  89. list.add(conn.createItemFromInt(2, null));
  90. list.add(conn.createItemFromInt(3, null));
  91. list.add(4);
  92. list.add(5);
  93. list.add(6);
  94. XQSequence sequence = conn.createSequence(list.iterator());
  95. XQPreparedExpression xqpe =
  96. conn.prepareExpression("declare variable $x as xs:int+ external; $x");
  97. xqpe.bindSequence(new QName("x"), sequence);
  98. XQResultSequence rs = xqpe.executeQuery();
  99. while(rs.next())
  100. System.out.println(rs.getItemAsString(null) + ", " + rs.getItemType());
  101. // Bind XQResultSequences to XQuery Expressions
  102. info("Bind XQResultSequences to XQuery Expressions");
  103. XQExpression expr = conn.createExpression();
  104. String path = new File("src/main/resources/xml").getAbsolutePath();
  105. String xqueryString =
  106. "for $x in doc('" + path + "/books.xml')//book/@isbn " +
  107. "return xs:string($x)";
  108. rs = expr.executeQuery(xqueryString);
  109. // Create a copy of the XQResultSequence that is scrollable and in memory.
  110. sequence = conn.createSequence(rs);
  111. expr.bindSequence(new QName("isbnCodes"), sequence);
  112. xqueryString = "declare variable $isbnCodes external; $isbnCodes";
  113. rs = expr.executeQuery(xqueryString);
  114. while(rs.next()) {
  115. System.out.println(rs.getItemAsString(null));
  116. }
  117. // Retrieve XML nodes
  118. info("Retrieve XML nodes");
  119. expr = conn.createExpression();
  120. xqueryString = "doc('" + path + "/books.xml')//book";
  121. rs = expr.executeQuery(xqueryString);
  122. while(rs.next()) {
  123. Node book = rs.getNode(); // org.w3c.dom.Element
  124. System.out.println(book);
  125. }
  126. // Create XML nodes
  127. info("Create XML nodes");
  128. // Create {@link Document} instance
  129. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  130. DocumentBuilder builder = factory.newDocumentBuilder();
  131. Document docObj = builder.newDocument();
  132. Element elementObj = docObj.createElement("e"); // from org.w3c.dom
  133. // Create an XQItem with a type of element()
  134. XQItem item1 = conn.createItemFromObject(elementObj, null);
  135. // Create an XQItem with a type of document-node()
  136. XQItem items2 = conn.createItemFromObject(docObj, null);
  137. System.out.println(item1.getItemType());
  138. System.out.println(items2.getItemType());
  139. // Retrieve date values
  140. info("Retrieve date values");
  141. xqueryString = "for $x in doc('" + path + "/books.xml')//publish_date " +
  142. "return xs:date($x)";
  143. rs = expr.executeQuery(xqueryString);
  144. while(rs.next()) {
  145. XMLGregorianCalendar cal = (XMLGregorianCalendar) rs.getObject();
  146. int day = cal.getDay();
  147. int month = cal.getMonth();
  148. int year = cal.getYear();
  149. System.out.println(year + "/" + month + '/' + day);
  150. }
  151. // Closing connection to the Database.
  152. close(conn);
  153. }
  154. }