I have a xml file and I want to store it contents in DB.How The format of my xml is not fixed 100%
I have xml file as shown below
<Jb>
<T1>false</T1>
<T2>
<email>dshetyo#adobe.com</email>
<userName>passwd</clusterName>
<passwd>adobe</clusterURL>
<url>dummy</url>
<param1>val </paramY>
<paramN> val </paramN>
</t2>
<fx>
<a >351108</a>
<b >4629 <b>
</fx >
</Jb>
I can have N params eg param1,param2 etc (N not fixed)How can i map it to a relatioanl table structure?
Any other approach?
This is a one to many relationship. You'll want a table like so:
account
id
email
usernmae
parameters
account_id
param_key
param_value
And then your SQL looks roughly like:
select * from account, parameters where account.id = parameters.account_id
Alternatively, you can use a single column to store all of your key value pairs as well. You can do this as a long string, or a clob (depending on what you think is more appropriate performance-wise and what your needs are in terms of size)
(so there would additional parsing necessary, but if you're not looking to select parameters via SQL, this is the way to go)
Usually, XML content is stored in a database in a single TEXT or VARCHAR column.
Because of its free-form structure, it is generally not suitable for O/R mapping.
One option is to insert the XML directly into the table, then use XPath commands to extract the fields on query. XPath is supported by the major databases: MySQL, Oracle, Postgres...
Oracle has XMLDB, their implementation of the XPath functions for searching based on XML. This means that you can define a column as being an XMLType data type, and attach/map schema to this (This aids in validating as well as evolving the XML over time).
XML in MySQL:
http://dev.mysql.com/tech-resources/articles/xml-in-mysql5.1-6.0.html
MySQL appear to support XPath functions as previously mentioned:
http://dev.mysql.com/doc/refman/5.6/en/xml-functions.html
But note, these features are still under development in MySQL.
Related
I'm trying to generate a Database using JOOQ
i do create a Table with this code:
CreateTableAsStep<Record> table = create.createTable("TestTable");
CreateTableColumnStep step = table.column("testColumn", SQLDataType.Integer);
step.execute();
this works fine, but when it comes to inserting data, i run into a problem
the doc includes the following example:
create.insertInto(AUTHOR)
.set(AUTHOR.ID, 100)
.set(AUTHOR.FIRST_NAME, "Hermann")
.set(AUTHOR.LAST_NAME, "Hesse")
.newRecord()
.set(AUTHOR.ID, 101)
.set(AUTHOR.FIRST_NAME, "Alfred")
.set(AUTHOR.LAST_NAME, "Döblin")
.execute();
here AUTHOR is not a simple String it expects a org.jooq.Table<R extends Record>
i thought there might be a return type when creating the table, but i did not find it. Google did not help as Table is not the best word to search for ;-)
Question: how can i get to an instance of a Table - i do have its name as String?
You can always create Table references via DSL.table(String) or DSL.table(Name). For example:
// Assuming this:
import static org.jooq.impl.DSL.*;
create.insertInto(table(name("TestTable")))
.set(field(name("testColumn")), 1)
.execute();
Notice also my usage of DSL.field(Name).
Plain SQL vs. Name references
It's worth reading up on the difference between creating dynamic table / field objects at runtime either with plain SQL strings (as in DSL.table(String)) or with name references (as in DSL.table(Name)). Essentially:
Plain SQL strings are case-insensitive and subject to SQL injection
Name references are case-sensitive by default
In your case, as you probably created case sensitive table/column names, you should prefer the latter. More info can be found here:
http://www.jooq.org/doc/latest/manual/sql-building/plain-sql
http://www.jooq.org/doc/latest/manual/sql-building/names
I have a requirement to get XML data stored in db2. In db2 the XML data is stored in a table column whose type is XML (db2).
Now if i have to retrieve it using plain JDBC (no other choice i have here), what is the java data type in which this should be mapped.
From what i researched, i feel it should be this way.
String xmlData = (String) resultSet.getObject(1);
(assuming 1 is the column index where the xml data is stored)
Is this a viable solution? Any suggestions?
ResultSet has getSQLXML(int) which returns a java.sql.SQLXML object.
SQLXML gives you the most options to access the XML data.
Anyway you have a wide variety of options to retrieve the XML.
According this doc you can simply use ResultSet.getString(int) if you want the data as String.
But as always it depends on the version of your DB2 and your JDBC driver if all these options are supported.
Hi am parsing an xml file through JAXB and saving the data in the database table and am able to do this appropriately. My question is if the xml file returns an empty data for a particular field it should display as === in the database table. How can I do this while processing the xml file.
The xml file has two nodes, abc and xyz and the xml file should contain any one of these.There are two coloumns available in the database say name and version. these two coloumns will be derived by comparing the abc and xyz nodes of teh xml file in the database by using a common id and fetch the values for name and title. Can some one please help me in understanding how to handle this by processing the xml file.
I Wish I could post the code, but the code is too huge to post it.
What you are asking sounds weird, but assuming your data is a String, you could do this:
if (data == null || data.isEmpty())
{
data = "===";
}
From the tags on your question you appear to have two steps in your processing:
XML to object using JAXB
object to database using ?
I would put the logic for storing === in the database as part of the object-to-database processing. If you are using JPA 2.1 (part of Java EE 7) you could look at JPA converters to encapsulate this logic.
I've been doing XML transformation for a while in my Java application and I'm still not clear what the options are and what the best option is to read records from a database and insert it in my transformed output file.
What I've been doing so far is querying an Oracle database with xQuery that gave me a nodeset as a result. I then passed this result as a parameter to the transformer and queried that parameter during the transformation to insert data into the appropriate nodes.
Is this the best way of doing it though? My base language again is Java. What would be the other options to get the same result?
Also I think it's worth mentioning that most of the times the db query is based on the content of the source XML file.
Thanks
Have you looked at the Saxon SQL extensions, in particular at <sql:query> extension element?
One possibility is to instantiate a Java object in your XSLT script;
<!-- Connection to the data provider. -->
<xsl:variable name="provider" xmlns:java="http://xml.apache.org/xalan/java"
select="java:my.sample.DataProvider.getInstance()" />
Using it to provide the data later on in the script:
<xsl:template match="node">
<xsl:variable name="mydata" xmlns:java="http://xml.apache.org/xalan/java"
select="java:getdata($provider, string(#attr))" />
This would call the method getData(String) on the object created by the static getInstance() method on your my.sample.DataProvider class.
You can use a setup like this to get values from a cache (for instance the query results you try to pass as parameter in your current setup), or to execute queries while transform is executed (preventing queries to data that is not visited by the transform.)
In java, do we have an open source to generate sql from an xml file? For example, the simplest case is :
<query>
<select>id</select>
<from>student</from>
<equal><column>name</column><val>john</val></equal>
<equal><column>age</column><val>23</val></equal>
<equal><column>registerDate</column><val>03/04/1990</val></equal>
</query>
Then it will generate a sql:
select id
from student
where name = 'john'
and age = 23
and registerDate = '03/04/1990'
Of course, this is simplest case, when we have between, join, it will be more complex. So do we have an open source to handle this? I don't want to use ORM like hibernate, because it goes too far.
Thanks.
Do you have to use this XML format? Was it somehow internally defined?
If your objective on XML is purely making dynamic queries easier to create, you may try MyBatis. It uses XML definition files to group the SQL queries together and allow you to dynamically change queries based on parameters, without the mess of assembling the SQL query with String concatenation. You won't define every element as a XML tag, but only parts of it that may change. Take a look at the MyBatis User Guide for more info and code samples. Here is a quick look at a simple dynamic query:
<select id=”findActiveBlogWithTitleLike” parameterType=”Blog” resultType=”Blog”>
SELECT * FROM BLOG
WHERE state = ‘ACTIVE’
<if test=”title != null”>
AND title like #{title}
</if>
</select>
That's just to give you a glimpse of what it would look like. There are plenty of possibilities and tags to help you build whatever query you may need, so I suggest you to take some time to read the user guide to fully understand the framework capabilities.