1

I have DB2 database with XML column. I would like to read data from it and save each XML to separate file.

Here is a part of my code:

final List<Map<String, Object>> myList = dbcManager.createQuery(query).getResultList();
    int i=0;
    for (final Map<String, Object> element : myList) {
        i++;
        String filePath = "C://elements//elem_" + i + ".xml";
        File file = new File(filePath);
        if(!file.exists()){
            file.createNewFile();
        }

        BufferedWriter out = new BufferedWriter(new FileWriter(filePath));
        out.write(element.get("columnId"));
        out.close();
    }

Now, I have error in line out.write(element.get("columnId"));, because element.get("columnId") is an object type and it should be for example string.

And my question is: To which type should I convert (cast) element.get("columnId") to save it in xml file?

1

1 Answer 1

1

You should use the ResultSet.getSQLXML() method to read the XML column value, then use an appropriate method of the SQLXML class, e.g. getString() or getCharacterStream(). More info here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.