I am using Tamino API for Java. When I try to update a xml object with a new string, the new string has all the < and > character replaced with < and >. When I try to execute a query on the db for the information I just updated, it is not found. Does any one know how to change the encoding to be strait ascii.
Thank you
Andy
Can you post your codes here? Maybe I can help you solve it.
public String update(String databaseURI, String collection, String xml,String updatedText)
{
// The database connection
TConnection connection;
// tamino response object
TResponse response;
String recordsaffected=null;
// Insert the XML document
try
{
// get connection object
connection = tcf.getConnection(databaseURI, collection);
// Obtain a TXMLObjectAccessor with a JDOM object model
accessor = connection.newXMLObjectAccessor( TAccessLocation.newInstance( collection ) , TJDOMObjectModel.getInstance() );
// create tquery object from xml string
TQuery query = TQuery.newInstance(xml);
// Invoke the query operation and obtain the response
response = accessor.query(query);
// create xmlobject from first retrieved object
TXMLObject xmlObject = response.getFirstXMLObject();
if ( xmlObject == null )
{
recordsaffected = “”+
“No Records found.”+
“”;
return recordsaffected;
}
// Obtain the JDOM element and update its content
Element element = (Element)xmlObject.getElement();
Debug.out(“Element text: “+element.getText()+” \nupdated text: “+updatedText);
element.setText(updatedText);
// Invoke the update
response = accessor.update(xmlObject);
recordsaffected = “”+
“”+response.getReturnValue()+””+
“”;
connection.close();
return recordsaffected;
}
catch (Exception e)
{
recordsaffected = “”+
“”+e.getMessage()+“”+
“”;
return recordsaffected;
}
}
Thank you,
Andy
Hi Andy,
could you please post a couple of other things too?
* The original XML document (the one that is being updated)
* The values of the parameters (especially “xml” and “updatedText”)
Thanks,
Trevor.
XML = 4<Name type="normal">Harry andrew Truman Drugs
updateText =
<?xml version="1.0" >4<Name type="normal">Novartis and James Drugs
Andy
Hi Andy,
the problem is that your code is replacing the value of the root node with a new document.
So the original document looks like this:
<Category><CatID>4</CatID><Name type="normal">Harry andrew Truman Drugs</Name></Category></pre><BR><BR>However, after the update the root node's value is replaced so that the document looks like this:<BR><pre class="ip-ubbcode-code-pre"><Category><?xml version="1.0"?><!DOCTYPE Category><Category><CatID>4</CatID>
<Name type="normal">Novartis and James Drugs</Name></Category></Category></pre><BR><BR>There are a number of ways in which this update can be done, but unfortunately this isn't one of them! (This approach replaces the text of the Category element with the text you provide in the call to element.setText() - which is a complete XML document.)<BR><BR>One way to accomplish your aim would be to update the whole document that is stored in Tamino:<BR><pre class="ip-ubbcode-code-pre">
if ( xmlObject == null ) {
recordsaffected = "<Result>" + "<Message>No Records found.</Message>" + "</Result>";
return recordsaffected;
}
// Obtain the JDOM element and update its content
//Element element = (Element)xmlObject.getElement();
//System.out.println("Element text: " + element.getText() + ...
//element.setText(updatedText);
// Create a new document with the given text
TXMLObject xmlObject2 = TXMLObject.newInstance(updatedText);
// Copy the Tamino ID from the old to the new document
xmlObject2.setId(xmlObject.getId());
// Update the document represented by the new object
response = accessor.update(xmlObject2);
</pre><BR><BR>A cleaner approach would be to just modify the necessary element(s) of the document:<BR><pre class="ip-ubbcode-code-pre">
// Obtain the JDOM element and update its content
Element element = (Element)xmlObject.getElement();
//System.out.println("Element text: " + element.getText() + ...
//element.setText(updatedText);
// Get the "Name" element
java.util.List names = element.getChildren("Name");
Element nameElement = (Element)names.get(0);
nameElement.setText("Novartis and James Drugs");
// Invoke the update
response = accessor.update(xmlObject);
(This is only a very simple solution, and will quite likely need to be enhanced to handle your real-world scenario.)
Some useful links to JDOM documentation can be found here: http://www.jdom.org/downloads/docs.html
I hope that helps,
Trevor.
Trevor,
I tried both of your suggestions and they both work. Thank you for help. You saved me a few gray hairs. I am new to Tamino, so this has been a real lession.
Kind regards,
Andy
Hi Andy,
you’re welcome - I’m glad that we could help you!
If you have any further questions I’m sure that someone in the Community will have the answers, so please don’t hesitate to post.
Have fun with Tamino,
Trevor.
I can update root node elements with the code you gave me. What is the syntax for updating child nodes?
Meaning:
Below is my schema. I would like to update the first “Author” node, changing “Kahn SE” to “Kahn ME” under /Document/BiblioData/Authors
Kind regads,
Andy
168000
Nateglinide
Kahn SE
Montgomery B
Howell WM
Lingueros-Saylan M
Hsu CH
Devineni D
McLeod JF
Horowitz AD
Diabetes
49 (Suppl.1) A113, abstr.457-P, 2000 (American Diabetes Assoc., 60th Sci. Sessions, San Antonio, US, June 9-13, 2000)
2000
Nateglinide increases first phase insulin secretion and glucose clearance
To determine whether nateglinide (NAT) is capable of increasing first phase insulin release and enhancing glucose tolerance, the authors measured beta-cell function and glucose tolerance following iv glucose administration in 21 subjects with type 2 diabetes given single NAT doses of 120 mg, glyburide (GLY) 10 mg or placebo in random order. Results (table) show that NAT increased first phase and stimulated earlier second phase insulin responses, without the markedly low plasma glucose levels observed with GLY. “Thus, NAT is a unique insulinotropic agent which improves glucose tolerance by enhancing early insulin release. The reduced risk of late hypoglycemia should make it useful in the treatment of type 2 diabetes.”
17
2000-06-21
0025
0
1
Andy
Hi Andy,
one approach would be much the same, but repeatedly retrieve child elements until the desired one is reached. Then it can be updated in the same manner.
Something like this:
...
if ( xmlObject == null ) {
recordsaffected = "<Result>" + "<Message>No Records found.</Message>" + "</Result>";
return recordsaffected;
}
// Obtain the JDOM element and update its content
Element documentElement = (Element)xmlObject.getElement();
java.util.List biblioList = documentElement.getChildren("BiblioData");
Element biblioElement = (Element)biblioList.get(0);
java.util.List authorsList = biblioElement.getChildren("Authors");
Element authorsElement = (Element)authorsList.get(0);
java.util.List authorList = authorsElement.getChildren("Author");
Element firstAuthor = (Element)authorList.get(0);
firstAuthor.setText("Kahn ME");
...
Cheers,
Trevor.
Thank you. That helps.
Andyu