Parsing XML Stored as Character Data

If your database does not support the XML type and you store XML documents as character data, you must parse the XML before it can be queried. This can be done with a Java external function called from within an XQuery query. The following Java external function creates a DOM tree by parsing its input:

  public static Document txt2xml(String txt) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {
      builder = factory.newDocumentBuilder();
    }
 
    catch (ParserConfigurationException e) {
      e.printStackTrace();
      return null;
    }
    Document doc = null;
    try {
      doc = builder.parse(new InputSource(new StringReader(txt)));
    }
    catch (SAXException e1) {
      e1.printStackTrace();
      return null;
    }
    catch (IOException e1) {
      e1.printStackTrace();
    }
    return doc;
  }

Using DataDirect XQuery®, you can call this function by declaring it in the prolog and using it in a query. For example:

declare namespace p='ddtekjava:txt2xml';
declare function p:txt2xml($inp as xs:string) as document-node() external;
for $row in collection('HOLDINGSXML')/HOLDINGSXML/XMLCOL
return 
   p:txt2xml($row/XMLCOL)/HOLDINGS/SHARE[@COMPANY='Amazon.com, Inc.']

Try DataDirect XQuery® Free!

Put the power, scalability, and performance of DataDirect XQuery® to work for you today! Our free trial lets you see for yourself how easy it is to build data integration applications that access relational, EDI, and other file formats as XML!

Download DataDirect XQuery® today!

Online Video Tutorials!

Our easy-to-follow online video tutorials are a great way to get acquainted with the many features of DataDirect XQuery®.

And if you like what you see, download a free copy today and try DataDirect XQuery® for yourself!

Stay Informed!

XQuery is one of the hottest XML technologies being developed today. Stay informed with vital news about standards, tools, and trends by signing up for the DataDirect XQuery® newsletter.