aspose file tools*
The moose likes XML and Related Technologies and the fly likes How to map two xml files in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to map two xml files in java" Watch "How to map two xml files in java" New topic
Author

How to map two xml files in java

Durga Roobini
Ranch Hand

Joined: Dec 19, 2013
Posts: 105

A.xml
<A>1</A>
<B>2</b>
<c>3</c>
.
.
<z>..</z>
B.xml
<a>3</a>
<b>1</b>
<c>2</c>
.
.
<z>..</z>
A.xml -> map to -> B.xml
<a> ---> <b>
<b> ---> <c>
<c> ---> <a>

Now i have 2 xml files name like A.xml and B.xml now i have to map these 2 xml files like below and display output in browser Please give me the brief solution to find out my problem
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41791
    
  62
If you search for "xml diff" or some such phrase you'll find numerous tools that help compare XML files. I'm not aware of one that will condense it down to the format you describe, though. You may have to come up with an algorithm and implement it yourself (possibly based on the output of one of those diff tools).


Ping & DNS - my free Android networking tools app
Durga Roobini
Ranch Hand

Joined: Dec 19, 2013
Posts: 105
different phrase of xml,in my case let assume A.xml is Standard it never going to change,but B.xml is dynamicaly it will change, based on that mapping will be happen, this is my scenerio,please guide me to look forward of my case,please tell me what algorithm i have to use ? suggest me some links to help my problem
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41791
    
  62
I don't think I understand the problem. A is mapped to b by virtue of them having the same child text? Yet the mapping you mention has only lowercase tags, so they're all from B.xml? What if the child text in A does not occur in B, or vice versa? Can child texts only have numbers, or can it be any text? What if there is whitespace in one file, but not in the other - would that be identical? And what, exactly, should the output be in all these cases?

Note that "mapping XML" is not a standard term, so you need to explain to use what should happen in detail before we can advise you on how to start designing the algorithm.
Durga Roobini
Ranch Hand

Joined: Dec 19, 2013
Posts: 105
i will answer your all question at level of my best
before explaining,
A.xml and B.xml are simple egs, that i believe make my question to be easier
1)No both of not having similar text,its different means
A.xml(it quite constant in all the cases)
<id></id>
<ename></ename>

B.xml(it will change dynamically)
<Event_Id>3234344</Event_Id>
<Event_Name>Seminars</Event_Name>
now i want to map B.xml to A.xml means i want write Event_Id=3234344 from B.xml to A.xml like id=3234344 and etc..

2)The child text A.xml is not in B.xml and Vice Versa in this case i simply mention null in both cases,but A.xml is main for me

3)Child Text contains both number and texts

4)Exact output what i want is standarsd xml(A.xml)->(Constant) which it is map from B.xml(dynamic)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41791
    
  62
I'm still confused. In your first post, A.xml had data. In your last post, it does not, and you mention wanting to populate A.xml with data from B.xml. #4 I don't understand at all - first you said A.xml was constant, but then you mention you want A.xml as output.

I think at this point the best would be for you to give several actual examples of both files (shortened as much as as possible), and what the result would look like.
Durga Roobini
Ranch Hand

Joined: Dec 19, 2013
Posts: 105
Sorry For make confusing,my last post is clear one,A.xml have no data this is my own xml it have only elements no attributes but B.xml have element and attributes and it is dynamic,so I want to map B.xml to A.xml please give me the solution soon I need very urgent
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Do you mean, you wish to extract elements from b.xml and merge it to a.xml?


Swastik
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41791
    
  62
Well, if I had thought that your previous post was clear (to me) I wouldn't have asked for examples. In that light it's unfortunate that you feel this is urgent, because in a volunteer-driven forum like this it's uncertain when you might get further answers, and how much those might help you. I advise to make your manager aware that you're struggling with this task.

Also, let's be clear about nomenclature - none of the XML you have posted has any attributes. I think you're talking about text nodes. This is an attribute: . This is a text node:

What I understand is this: You have an XML file A.xml which has just tags in it, but no data. The names of those tags will always be the same. You also have an XML file B.xml which does have data in text nodes, but which has different tag names. What you wish to create is a file C.xml (or maybe just an in-memory representation of it) that has the tag names of A.xml, but which contains the text nodes that correspond to certain elements in B.xml (which could have different names each time). Is that a correct description?
Durga Roobini
Ranch Hand

Joined: Dec 19, 2013
Posts: 105
ya Exactly! you are great thanks for understand my problem please tell me the way to solve this one,or this way will not work means tell alternative way without changing concept
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41791
    
  62
It's software - of course it's possible to make it work somehow. Whether the concept is valid only you can say anyway, as we only know *what* you intend to do, not *why* you intend to do it.

I would probably load both A.xml and B.xml as org.w3c.dom.Document objects into memory, and then use XPath to extract the relevant data from B, and insert it via DOM methods into A. Obviously, at this point your code needs to know somehow what the relevant element names are in B, and to which elements in A they correspond (the "mapping" which you mentioned).
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 500
    
    5
As an alternative solution, you can look into XSLT (Xml Stylesheet Transformation). It's a technique to transform XML data from one schema to another.

For example, if I've understood the discussion correctly, what you want to do is convert B.xml like


to A.xml with a different schema, like:


Is this what you are looking for? If so, XSLT would be a code reducing alternative to manipulating DOM and XPath in code.

Also, what exactly do you mean by B.xml is dynamic? Are the tags different in each B.xml, or are the values different?

It would be very easy to answer precisely, if you can show us the exact B.xml file(or files) you have (not the "simplified" one...that's just adding to the confusion right now) and the exact A.xml you want out of them. By exact, I mean a well formed valid XML file (like in my examples above).
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41791
    
  62
Karthik Shiraly wrote:Also, what exactly do you mean by B.xml is dynamic? Are the tags different in each B.xml, or are the values different?

I think the element names are different. While that doesn't rule out using XSLT, it makes it more difficult because a different stylesheet would be needed for each different version of B.xml - that's why I think a DOM approach of some kind would be simpler.
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 500
    
    5
Ulf Dittmer wrote:I think the element names are different. While that doesn't rule out using XSLT, it makes it more difficult because a different stylesheet would be needed for each different version of B.xml - that's why I think a DOM approach of some kind would be simpler.

If there are different versions of B.xml with different schemas, would they not require multiple pieces of DOM manipulating logic? Perhaps I've misunderstood, but I'm not able to visualize an example when DOM manipulation would be simpler (in terms of reading/writing/maintaining, though I readily admit that XSLT constructs too are complex, and I find myself referring to w3schools all the time whenever I use XSLT!). Personally, I find DOM manipulation with its multiple nested for loops, findElementsByTagName(), type casts, and comparison of node names more difficult to read or write or maintain (in case of future of schema changes), compared to XSLT.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41791
    
  62
I agree, but note that I didn't say "DOM manipulation", I said "DOM approach of some kind" :-) The approach I mentioned earlier would create a DOM Document object, and then use XPath to extract the actual data. An XPath expression would be relatively easy to adapt to different element names.
Durga Roobini
Ranch Hand

Joined: Dec 19, 2013
Posts: 105
Karthik Shiraly wrote:As an alternative solution, you can look into XSLT (Xml Stylesheet Transformation). It's a technique to transform XML data from one schema to another.

For example, if I've understood the discussion correctly, what you want to do is convert B.xml like


to A.xml with a different schema, like:


Is this what you are looking for? If so, XSLT would be a code reducing alternative to manipulating DOM and XPath in code.

Also, what exactly do you mean by B.xml is dynamic? Are the tags different in each B.xml, or are the values different?

It would be very easy to answer precisely, if you can show us the exact B.xml file(or files) you have (not the "simplified" one...that's just adding to the confusion right now) and the exact A.xml you want out of them. By exact, I mean a well formed valid XML file (like in my examples above).





Thanks a lot Sir,I use XSLT and I got output in A.xml from B.xml,like you mention in your sample xml code in above.but my second problem is B.xml is dynamic means my second B.xml is look like

<Event>
<Asset_ID>4883748374</Asset_ID>
<Asset_name>Seminars</Asset_Name>
</Event>
I want this data to A.xml without changing anything in XSLT
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 500
    
    5
I'm still not sure what you mean by "dynamic", but looking at B.xmls in above posts, I infer that Ulf is right - the schema of each input file may be different.

If so, then as already pointed out, "...your code needs to know somehow what the relevant element names are in B, and to which elements in A they correspond".
If you don't want your code or XSLT to change in future, then you need to have with you all the possible schemas that your input files can take, go through those schemas
and see what tricks to apply to extract information.

For example, if you find that every input file you know of has an <Event> element with a single <*_id> element, and that <*_id> element value is always the event id, then
you know your logic should look for tags that match <*_id>.

Every possible input schema will require an "adapter" which has the logic to
1) Either recognize the schema and decide whether it can handle this schema, or pass on the responsibility to next adapter. Or if the app knows the source of the input xml, it can directly invoke the appropriate adapter.
2) adapter searches for required information in input file
3) adapter converts them to output schema

You can implement these adapters in 2 ways:
1) write java code to extract information using XPath APIs, and write them out using DOM APIs
or
2) write XSLT to transform information (it too uses XPath constructs) and write them out

Whichever way you choose, implementing a separate adapter for each and every input schema would be high effort - coding and maintenance - because things like the
output schema would get hardcoded into each and every adapter, either by java code or by XSL code.

So, you'll have to look at tricks to search what is common to all these input files. Some possible tricks you can use:
1. If you know that id is always in <event_id>, <asset_id>, <e_id> or some other limited set of tags, use the "or" operator:


2. To search for all tags that match regular expression like <*_id>, use the matches() operator:


3. Since output schema is same regardless of input, it would be best to have it in a single place and not duplicate it .
So, if doing all this the java way, keep the output schema writing logic in a separate module and use the above regex/OR type queries to minimize
input processing.
If doing all this using XSLT, then <xsl:include> will help to keep the output schema and input rules separated. Each input schema gets its own set of templates and
these are applied only if the node matches. Use the same XPath tricks above to minimize number of rules.
Or if the XSLT invoking program/servlet knows the source of each input file, it can pass in an approriate XSL paramter and help the transformer rules that way. The input templates can then apply
their rules only if that parameter has expected value.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41791
    
  62
I suspect the problem goes much deeper than that, though. Judging by some of the questions Durga has asked previously (like http://www.coderanch.com/t/626438/XML/Multiple-URL-access), I'm guessing that the XML files in question may have entirely different schemas, not just different tag names within the same schema. If that is indeed the case, the problem would be much harder (and the answers to the previous question would apply). Maybe Durga can clarify if it is indeed the same problem.
Durga Roobini
Ranch Hand

Joined: Dec 19, 2013
Posts: 105
Karthik Shiraly wrote:I'm still not sure what you mean by "dynamic", but looking at B.xmls in above posts, I infer that Ulf is right - the schema of each input file may be different.

If so, then as already pointed out, "...your code needs to know somehow what the relevant element names are in B, and to which elements in A they correspond".
If you don't want your code or XSLT to change in future, then you need to have with you all the possible schemas that your input files can take, go through those schemas
and see what tricks to apply to extract information.

For example, if you find that every input file you know of has an <Event> element with a single <*_id> element, and that <*_id> element value is always the event id, then
you know your logic should look for tags that match <*_id>.

Every possible input schema will require an "adapter" which has the logic to
1) Either recognize the schema and decide whether it can handle this schema, or pass on the responsibility to next adapter. Or if the app knows the source of the input xml, it can directly invoke the appropriate adapter.
2) adapter searches for required information in input file
3) adapter converts them to output schema

You can implement these adapters in 2 ways:
1) write java code to extract information using XPath APIs, and write them out using DOM APIs
or
2) write XSLT to transform information (it too uses XPath constructs) and write them out

Whichever way you choose, implementing a separate adapter for each and every input schema would be high effort - coding and maintenance - because things like the
output schema would get hardcoded into each and every adapter, either by java code or by XSL code.

So, you'll have to look at tricks to search what is common to all these input files. Some possible tricks you can use:
1. If you know that id is always in <event_id>, <asset_id>, <e_id> or some other limited set of tags, use the "or" operator:


2. To search for all tags that match regular expression like <*_id>, use the matches() operator:


3. Since output schema is same regardless of input, it would be best to have it in a single place and not duplicate it .
So, if doing all this the java way, keep the output schema writing logic in a separate module and use the above regex/OR type queries to minimize
input processing.
If doing all this using XSLT, then <xsl:include> will help to keep the output schema and input rules separated. Each input schema gets its own set of templates and
these are applied only if the node matches. Use the same XPath tricks above to minimize number of rules.
Or if the XSLT invoking program/servlet knows the source of each input file, it can pass in an approriate XSL paramter and help the transformer rules that way. The input templates can then apply
their rules only if that parameter has expected value.



I understood what you said but am confused you told two ways
1)which one is efficient Using Java Code with Xpath or XSLT?
2) What you mean adapter is this XML adapter?
3)if i using xslt means can i able to compare tags ?
3a) how to use regular expression in xslt?
3b)can i able to send multiple parameters from java to xslt?
3c)what are all tags i need for that?
sorry for ask all things to you but i need it, you provide few keywords correspond to my question is enough then i google it and search myself please help me
Because newbie to xml and xslt and xpath parts please give sequence of idea to implement my thought
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 500
    
    5
1) which one is efficient - Using Java Code with Xpath or XSLT?

In terms of performance, I would guess (because I've not measured) they are both comparable, because XSLT too uses XPath. Anyway, you should write prototypes and measure both if
performance is critical.

In terms of coding and maintaining effort, I would say it totally depends on 1) how many different input schemas you have 2) how complex is each input schema 3) are the schemas
amenable to some kind of input processing tricks.

Take a simple example:


If you want to convert this to output schema using Java code, it would look something like:


The equivalent XSLT would be

and a trivial 4-line piece of java code to invoke that XSLT on the source document using Transformer API.

Note that all the "select=" experessions are XPath expressions. So in either approach, you are still using XPath.

For the above trivial example, you can see that the XSLT saves you from all the for loop iterations for each level in the XML, element name checks, etc. As the input schema becomes
more complex, the amount of java code required to process it also becomes more complex. XSLT is just a more concise, declarative way of achieving the same thing.

Some advanced processing like regular expressions or string matches can be done in XSLT itself. For example, you can do things like:


But when there are multiple input schemas, there is a chance that using the java code based approach may allow you to do more tricks than XSLT allows.
In XSLT, you are limited by what operators and functions that XSLT provides, but when using java code there are no such limitations. You can use any kind of "if" conditions
to match multiple input schemas.
Which one is better for you only you can say, by looking into all your possible input XML schemas.

2) What you mean adapter is this XML adapter?

"Adapter" is a design pattern. If it confuses, then ignore it; it's not very important to the discussion.

3)if i using xslt means can i able to compare tags ?

An XSLT expression like select="Event" is doing a comparison.

3a) how to use regular expression in xslt?

Go to http://www.w3schools.com/xpath/xpath_functions.asp and search for "matches" and "ends-with".

3b)can i able to send multiple parameters from java to xslt?

You can pass any number of parameters, using transformer.setParameter()

3c)what are all tags i need for that?

For what? For accessing parameter, it's <xsl:param>. For using in conditions, it's "<xsl:if test="$paramname='value'">"
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 500
    
    5
Sorry, just realised that even the java code can be made more concise by using XPath all the way through, like this:
>
Durga Roobini
Ranch Hand

Joined: Dec 19, 2013
Posts: 105
Karthik Shiraly wrote:Sorry, just realised that even the java code can be made more concise by using XPath all the way through, like this:
>




Thanks alot,but i struck with some place in above coding,i write codings based on my xml and transform it to xml but it not get nodevalue my output is like this now,

<?xml version="1.0" encoding="UTF-8"?>



my java codings is
>
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 500
    
    5
Unfortunately, looks like you have totally misunderstood my post.
What I have shown in my post are *examples*. They were meant only to demonstrate and compare 2 *alternate* techniques of transforming one schema to another schema. Please note - they are *alternate* techniques. I don't see any benefit in combining them.
You should go through the post, understand the differences, and then apply the concepts to your own XMLs. It's not code meant to directly solve your specific problem.
In fact, I think you should stop coding, and instead analyze the problem - specifically, how many different types of input schemas you have and come up with ideas to process them. Once the ideas are clear, you can then prototype both and see which of the above method will be better for your specific problem.
Durga Roobini
Ranch Hand

Joined: Dec 19, 2013
Posts: 105
really am confusing ,clearly my whole problem is ,I get xml responses from third party API means eventful.com,eventbrite.com,yahooevents like this, I have an own xml as I mention above is A.xml-,my incoming xml responses is B.xml-->dynamic(eventful,eventbrite,..etc),based on client request, I pass specific url from my local database ,it get some xml responses that I try to map with my own xml file (A.xml file),I think xpath concepts works ,however I don't want xml schema I have to send my owm standard and generic xml(A.xml ->it should be common for all API providers) to client and also unmatched tags also there I make null values that time ,once I try solve these issue means I get confidence to solve upcoming issue,really am newbie and fresher to java and xml please guide me
Durga Roobini
Ranch Hand

Joined: Dec 19, 2013
Posts: 105
Karthik Shiraly wrote:Unfortunately, looks like you have totally misunderstood my post.
What I have shown in my post are *examples*. They were meant only to demonstrate and compare 2 *alternate* techniques of transforming one schema to another schema. Please note - they are *alternate* techniques. I don't see any benefit in combining them.
You should go through the post, understand the differences, and then apply the concepts to your own XMLs. It's not code meant to directly solve your specific problem.
In fact, I think you should stop coding, and instead analyze the problem - specifically, how many different types of input schemas you have and come up with ideas to process them. Once the ideas are clear, you can then prototype both and see which of the above method will be better for your specific problem.


I got some idea ,ya I have different types of input schemas I believe your idea, that types input schema can map my xml file(A.xml) one by one and make generic xml file,i mean regular expression is key to sove and match tage from different schemas

based on your idea my output A.xml is

so here,B.xml is Eventful
for other xml schemas I try to use regex to filter or match tags
Durga Roobini
Ranch Hand

Joined: Dec 19, 2013
Posts: 105
Durga Roobini wrote:
Karthik Shiraly wrote:Sorry, just realised that even the java code can be made more concise by using XPath all the way through, like this:
>




Thanks alot,but i struck with some place in above coding,i write codings based on my xml and transform it to xml but it not get nodevalue my output is like this now,

<?xml version="1.0" encoding="UTF-8"?>



my java codings is
>




I follow the above coding based on my program sceneria and transform it to xml document,in the above coding my ref.xml file have description tag it contailns paragraph ,all other tags transform xml format but when I add description tag it not come xml format more than all other tags of that attributes come like text output what I do for this
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to map two xml files in java