wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes simple dom4j xml problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "simple dom4j xml problem" Watch "simple dom4j xml problem" New topic
Author

simple dom4j xml problem

shai ban
Ranch Hand

Joined: Jan 05, 2010
Posts: 177
Hi all,
I have a schema like below...





Now I want to insert a element in that schema like below...


The logic I wrote for converting it is first create a 'group1' element and pick each element from the source document and detach them and add them into this 'group1' element. Snippet is mentioned below...


The problem is, if I doesn't have namespace in my original schema then the nodecount()is coming one and everything is going fine. But if I define namespace like above the first schema, then nodecount() comes two and first node is namespace itself that doesn't get detached after the statement 'detach()' and it loop keeps executing resulting a memory exception!!!
I am not able to understand what is this problem. Why namespace is coming as a element like DESCRIPTION?

Thanks.



Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You're trying to automate simple changes to a schema? That seems pointless to me; in real life schemas are rarely changed, and when they are, it's done by a designer who will edit the schema and publicize the new version. And besides, the schema you posted there doesn't belong to you.

As for your question "why?", that's also pointless. You have observed that Dom4J produces namespace nodes which you have to deal with. So deal with them. Asking why the designers decided to do it that way isn't going to help.
shai ban
Ranch Hand

Joined: Jan 05, 2010
Posts: 177
Paul Clapham wrote:You're trying to automate simple changes to a schema? That seems pointless to me; in real life schemas are rarely changed, and when they are, it's done by a designer who will edit the schema and publicize the new version. And besides, the schema you posted there doesn't belong to you.

As for your question "why?", that's also pointless. You have observed that Dom4J produces namespace nodes which you have to deal with. So deal with them. Asking why the designers decided to do it that way isn't going to help.


Thanks for the reply.
As far as your concern about the schema changes in real life, there might be multiple needs eg. you want to change the schema in certain conditions but you want to have original one also for backward compatibility.
2nd thing, if I am trying to iterate on rootElement (that is 'schema') then it should give me only one node 'description' as per my schema not two?

If you have some solution to implement this then please let me know.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

shai ban wrote:As far as your concern about the schema changes in real life, there might be multiple needs eg. you want to change the schema in certain conditions but you want to have original one also for backward compatibility.


Yes, that's true. But what you are trying to do there could have (and should have) been done with a text editor. Would have taken under a minute and it would be done by now.

2nd thing, if I am trying to iterate on rootElement (that is 'schema') then it should give me only one node 'description' as per my schema not two?
If you have some solution to implement this then please let me know.


You're still asking questions about "should". If you think that Dom4J isn't working the way it "should" then you should let them know. But the most likely situation is that it is working as designed and you are making incorrect assumptions about how it works.

So my suggestion is that you write your code based on how Dom4J actually works and not based on how you want it to work. In particular you are going to have to look at the type of the nodes you get and write code to behave accordingly.
shai ban
Ranch Hand

Joined: Jan 05, 2010
Posts: 177
Paul Clapham wrote:
shai ban wrote:As far as your concern about the schema changes in real life, there might be multiple needs eg. you want to change the schema in certain conditions but you want to have original one also for backward compatibility.


Yes, that's true. But what you are trying to do there could have (and should have) been done with a text editor. Would have taken under a minute and it would be done by now.

2nd thing, if I am trying to iterate on rootElement (that is 'schema') then it should give me only one node 'description' as per my schema not two?
If you have some solution to implement this then please let me know.


You're still asking questions about "should". If you think that Dom4J isn't working the way it "should" then you should let them know. But the most likely situation is that it is working as designed and you are making incorrect assumptions about how it works.

So my suggestion is that you write your code based on how Dom4J actually works and not based on how you want it to work. In particular you are going to have to look at the type of the nodes you get and write code to behave accordingly.


Ya, I already have done this. But I posted in this thread to share my experience and to know the suggestion from others. There might some optimized technique to do this. that's why....
shai ban
Ranch Hand

Joined: Jan 05, 2010
Posts: 177
so nobody have optimized way to achieve that?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

When you say "optimized", exactly what did you have in mind? Are you asking us to help you choose between several solutions which you have developed for this problem? If so, you should at least tell us what they are. Or if not, then what?
shai ban
Ranch Hand

Joined: Jan 05, 2010
Posts: 177
:)

I am preparing a seperate node with the 'group1' element, so...
- can I achieve this without creating a seperate a node? I mean can I modify the original schema according to my requirement?
- if schema is having a lot of elements then it will involve a lot of overhead in detaching/adding these elements in a new NODE. So is there any optimized way?

Hope, you will get what I am expecting. Thanks for you time.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Really, this isn't a problem which is worth spending so much development time on. Find a solution and implement it. Then move on to whatever is next. Don't waste your time on "optimized versions" of code which is trivial and does almost nothing.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

shai

My experience of schemas leads me to ask why you are changing such a file with code?

Schemas define a contract which if changed, are done so manually. These changes then generally lead to code changes for the publisher (party which owns the schema) and clients (or consumers) of that schema.
shai ban
Ranch Hand

Joined: Jan 05, 2010
Posts: 177
James Boswell wrote:shai

My experience of schemas leads me to ask why you are changing such a file with code?

Schemas define a contract which if changed, are done so manually. These changes then generally lead to code changes for the publisher (party which owns the schema) and clients (or consumers) of that schema.


As I mentioned above also to Paul, this is the requirement. And you can do it if requirement comes and by the way, we are doing this at runtime for a specific requirement that's why I can't change schema permanently.
I implemented it like the code I mentioned in my first post. But want some optimized solution or any other solution at code level.

Thanks a lot.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

What, specifically, makes you think your current solution is not "optimized"?
shai ban
Ranch Hand

Joined: Jan 05, 2010
Posts: 177
Paul Clapham wrote:What, specifically, makes you think your current solution is not "optimized"?


Because generally we ignore Node manipulation as it includes a lot of overhead.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: simple dom4j xml problem
 
Similar Threads
Jdom namespace issue
wsdl schema import problem
Unable to refer global element
Question about the document style message part example in Ivan's study guide
Need to modify xml using dom4j