If I have a XML Doc as following:
How to change it into DOM and insert this XML Object to Tamino XML DB?
Is it enough to do that by Tamino API for JAVA?
I know the following code can insert XML Object to DB:
TXMLObject newPatient = TXMLObject.newInstance(patient123);
response = accessor.insert(newPatient);
However, how to change the XML Doc into DOM(e.g. patient123)?
If you’re asking how to get raw xml into a DOM, that really depends on the Object Model you are using and the original form of your xml (String, File etc). If you’re using DOM (as opposed to JDOM) you can try:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document myDoc = builder.parse(source); </pre><BR>Your source can be a java.io.File, an InputSource, an InputStream or a URI.<BR>If your xml is a String you can use:<BR><pre class="ip-ubbcode-code-pre">Document myDoc = builder.parse(new ByteArrayInputStream(source.getBytes()));
An alternative is to create a TXMLObject directly from a java.lang.String:
String patientrawxml = "<patient><name .... "; ... TXMLObject patient = TXMLObject.newInstance(patientrawxml); </pre><BR><BR>Then you can insert this. <BR><BR>If you want to manipulate the TXMLObject as a DOM object before inserting it you can use the getDocument() method of TXMLObject, e.g<BR><pre class="ip-ubbcode-code-pre"> TXMLObject patient = TXMLObject.newInstance(patientrawxml); Document patientdom = (Document)patient.getDocument(); // do stuff using DOM ...
remembering to cast the Object returned from getDocument() to a Document object.
There is a small advantage to Bills’ suggestion in that you can utilise Xerces specific parser features if this is a requirement.
For more information on Xerces, DOM and JAXP please have a look at http://xml.apache.org/xerces2-j/api.html.
Software AG (UK) Ltd.
Thx 2 Special Advisors,
In Stuart reply, which stated that “// do stuff using DOM …”, did you imply that the document “patientdom” didn’t recognize the document structure I inputted so that I need to do somethings, e.g. define back the root, childs, etc., or you just mean to edit/manipulate the XML document content?
I just mean to edit/manipulate, e.g. if you want to change anything prior to inserting it.
Software AG (UK) Ltd.
I got it!!
Hi 2 Advisor,
Do you know any methods can parse Schema to build a DOM Tree?? I mean how can I establish the XML structure by the given Schema dynamically.