コード例 #1
0
  @Test
  public void xqueryTest()
      throws IOException, ClassNotFoundException, XQException, IllegalAccessException,
          InstantiationException, JAXBException {

    XQPreparedExpression expr;
    XQConnection conn;

    JAXBContext jaxbContext;
    Unmarshaller jaxbUnmarshaller;

    String xquery =
        "declare variable $doc  external;\n"
            + "for $r in $doc /response/body/resultat/actes/* \n"
            + "order by $r\n"
            + "return\n"
            + "<acte>\n"
            + "  <id>{$r/id/text()}</id>\n"
            + "  <name>{$r/nom/text()}</name>\n"
            + "  <init_date>{$r/data/data_inici/text()}</init_date>\n"
            + "  <start_time>{$r/data/hora_inici/text()}</start_time>\n"
            + "  <type>{$r/classificacions/nivell/text()}</type>\n"
            + "  <localization>{$r/lloc_simple/nom/text()}</localization>\n"
            + "  <street>{$r/lloc_simple/adreca_simple/carrer/text()}</street>\n"
            + "  <street_num>{$r/lloc_simple/adreca_simple/numero/text()}</street_num>\n"
            + "  <district>{$r/lloc_simple/adreca_simple/districte/text()}</district>\n"
            + "  <CP>{$r/lloc_simple/adreca_simple/codi_postal/text()}</CP>\n"
            + "  <x>{$r/lloc_simple/adreca_simple/coordenades/geocodificacio/data(@x)}</x>\n"
            + "  <y>{$r/lloc_simple/adreca_simple/coordenades/geocodificacio/data(@y)}</y>\n"
            + "</acte>";

    url = new URL("http://w10.bcn.es/APPS/asiasiacache/peticioXmlAsia?id=203");

    URLConnection urlconn = url.openConnection();
    urlconn.setReadTimeout(50000);

    XQDataSource xqds =
        (XQDataSource) Class.forName("net.sf.saxon.xqj.SaxonXQDataSource").newInstance();
    conn = xqds.getConnection();
    expr = conn.prepareExpression(xquery);
    expr.bindDocument(new javax.xml.namespace.QName("doc"), urlconn.getInputStream(), null, null);

    jaxbContext = JAXBContext.newInstance(Acte.class);
    jaxbUnmarshaller = jaxbContext.createUnmarshaller();

    OutputStream testFile = new FileOutputStream(testXmlRoute);

    List<Acte> actes = getActes(jaxbUnmarshaller, expr, conn);

    System.out.print(getActes(jaxbUnmarshaller, expr, conn));

    assertTrue(!actes.isEmpty());
  }
コード例 #2
0
  // @Test
  public void staticFileTest()
      throws ClassNotFoundException, IllegalAccessException, InstantiationException, XQException,
          FileNotFoundException {

    XQPreparedExpression expr;
    XQConnection conn;
    // TODO Change local path file
    InputStream testFile = new FileInputStream(testXmlRoute);
    String xqueryString =
        " declare variable $doc external;\n" + "for $x in $doc return $x//acte/nom/text()";

    XQDataSource xqds =
        (XQDataSource) Class.forName("net.sf.saxon.xqj.SaxonXQDataSource").newInstance();
    conn = xqds.getConnection();
    expr = conn.prepareExpression(xqueryString);
    expr.bindDocument(new javax.xml.namespace.QName("doc"), testFile, null, null);

    XQResultSequence rs = expr.executeQuery();
    System.out.println("test:");
    while (rs.next()) System.out.println(rs.getItemAsString(null));
    assertNotNull(rs);
    conn.close();
  }