简体   繁体   中英

Java: How do I parse XML Type Column Data from Oracle DB Table?

I'm trying to receive a Column where the Oracle Data Type is XMLType

I have scoured several resources and they have all lead me to obtaining the following jars from Oracle:

ojdbc8.jar xdb6.jar and lastly, xmlparserv2.jar

OraclePreparedStatement stmt = (OraclePreparedStatement) connection.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                XMLType poxml = (XMLType)rs.getObject("XML_DATA");//breaks here
                String poString = poxml.getStringVal();
}

First, it claims that getStringval() is deprecated. More Concerning is when I finally obtained xmlparserv2.jar, I would get the following error that prevents the application from ever starting:

org.xml.sax.SAXNotRecognizedException: SAX feature 'http://apache.org/xml/features/allow-java-encodings' not recognized.
    oracle.xml.jaxp.JXSAXParserFactory.setFeature(JXSAXParserFactory.java:260)

Surely there's a way for these Java and Oracle DB to work together? They're both owned by the same company.

There are ways to get XMLType data from oracle db-

  • Use getOPAQUE() call in the PreparedStatement to get the whole XMLType instance, and use the XMLType constructor to construct an oracle.xdb.XMLType class out of it. Then you can use the Java functions on the XMLType class to access the data.
  • Use the getClobVal(), getStringVal() or getBlobVal(csid) in SQL and get the result as an oracle.sql.CLOB, java.lang.String or oracle.sql.BLOB in Java

Please try below code :

      OraclePreparedStatement stmt = (OraclePreparedStatement)
      conn.prepareStatement("select e.poDoc from po_xml_tab e"); 
       ResultSet rset = stmt.executeQuery(); 
       OracleResultSet rs = (OracleResultSet) rset; 

       while(rs.next())
    {
    XMLType poxml = XMLType.createXML(rs.getOPAQUE("column_name")); 

    Document podoc = (Document)poxml.getDOM(); 
    //convert document object to string
        String xmlString =getStringFromDocument(podoc)
    }
-----------------------------------------------------------------------------    

//method to get String from Document 
private String getStringFromDocument(Document doc)
{
    try
    {
       DOMSource domSource = new DOMSource(doc);
       StringWriter writer = new StringWriter();
       StreamResult result = new StreamResult(writer);
       TransformerFactory tf = TransformerFactory.newInstance();
       Transformer transformer = tf.newTransformer();
       transformer.transform(domSource, result);
       return writer.toString();
    }
    catch(TransformerException ex)
    {
       ex.printStackTrace();
       return null;
    }

I figured out how to extract and XML type from a Result Set:

First, I acquired the following JARs from Oracle:

ojdbc8.jar

xdb6.jar

xmlparserv2.jar - this jar I had to do a little digging to find this jar(unfortunatly, I don't have the source off hand. I remember having to get a .zip file where it was buried in a few folders)

I made a lib folder in my project directory in Intellij(on the same level as my src folder)

. I added them to Maven Manually doing the following:

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>xdb</artifactId>
            <version>6</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/xdb6.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc</artifactId>
            <version>8</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/ojdbc8.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>xmlparserv</artifactId>
            <version>2</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/xmlparserv2.jar</systemPath>
        </dependency>

I added the jars to the Artifact manually(not sue if there's a different way to do this?) by going to Project Structure -> Artifacts and added them to my war file(Right click and say "Add to /WEB-INF")

From there, the code might look something like this:

        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
            XMLType poxml = (XMLType) rs.getObject("XML_DATA");
            String sDMPayload = poxml == null ? null : poxml.getStringVal();
        }

I cast it to a string, because I would then deserialize the XML into an object later

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM