• Post Reply Bookmark Topic Watch Topic
  • New Topic

xml payload inside a String object  RSS feed

 
Mohit Sinha
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Currently I have a method which has a String input parameter.
The xml payload arrives something like this
<?xml version="1.0"?><tagparent><tagchild>yes</tagchild>
other tags
</tagparent>

Now I want to do some further processing on this string xmlpayload I recieve & pass the payload to another method.

This further processing method works when I pass the xml as
<tagparent><tagchild>yes</tagchild> </tagparent>

but won't work when the xml is
<?xml version="1.0"?><tagparent><tagchild>yes</tagchild> </tagparent>


Do I need to remove the initial xml declaration tag before sending it to the method as it has doublequotes within it.

If anyone has encountered a similar scenario do let me know your suggestions.
Currently I m just thinking of removing the xml declaration doing some substring & replace but i don't feel this is an elegant solution.

Kindly post your thoughts on the same


Regards,
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My thought: if this other method that you mention claims to accept an XML document, and it acts the way you describe, then it needs to be fixed. But you don't say that it claims that. In fact you don't say anything about it, when it is actually the source of your problem.
 
Mohit Sinha
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,

Thanks for your response.
The other method accepts the xml payload as a String input & adds a few more tags. This modified xml is then sent to the actual xml parsers.

My problem is in the modify method which is receiving the xml input along with xml declaration & cannot process correctly when there is a xml declaration in the payload as i mentioned in my earlier post.

Is there an xml way to this like using a Java xml parser class to just remove the xml declaration & then convert it back to string & pass on

Do post your comments

Regards,
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

My problem is in the modify method which is receiving the xml input along with xml declaration & cannot process correctly when there
is a xml declaration in the payload as i mentioned in my earlier post.

You must be using non-standard parser, all the standard parsers honor xml declaration.
 
Mohit Sinha
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Ajay ,

I complicated that.

To put it in a more simple manner.

My java class method recieves a string input which is an xml as I mentioned above.

Now i have to send this xml(which is recieved as string) to another utility class for transformation, parsing & validation.

Now before i send it to the xml utility I have to interspere one or two tags to the existing xml (which I recive as string).
This method which accepts a string input (xml payload) & does the job of adding a few more tags fails when the input string (which recives the xml) has a xml declaration. When i say it fails it does not give an exception but just does not add the extra tags.
If I donot have the xml declartion in the string input then it works fine.

So my question is whether its a good idea to remove the xml declartion by doing substring & replace.


Regards,
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no, that's not a good idea.
Figure out why it fails if it is marked as being xml (which should give you a strong hint as to why it fails) and fix that.
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its not good idea to remove xml declaration. post the code that is adding new nodes.
 
Mohit Sinha
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the codepiece which I am using for the same

public static String addXmlPayload(String origxml, String addxml)
{
Pattern p = Pattern.compile("(<(.*?)>.*)(</\\2> ", Pattern.DOTALL);
Matcher m = p.matcher(origxml);
if (m.matches()) {
return m.group(1) + addxml + m.group(3);
}
else return origxml;
}


Regards,
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are not using xml parser! Use one that would eliminate a lot of issues, You can start with jdom.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's bring everyone up to speed on this... here is the original JR topic that created that "parser".

Now, you are asking for a slight modification of the "parser" so that it will ignore one xml header. This is simply a one minute modification, but I am hesitant, as I think this project will slowly add more or more xml requirements -- it may be better to switch to a full xml parser now.

Henry
[ January 08, 2007: Message edited by: Henry Wong ]
 
Mohit Sinha
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. We already have xml document parsing where its imperative. Like after we fetch this xml we run it against a xsl transformation to produce the new xml as required by the external system. Then this new xml is subjected to dtd validation where it check if its compliant with dtd as published by the external system.
All this processing happens within something called as event listeners of a content management system where in we take these events(xml payload), transforms them & send those events to the external system in their xml format.

Why we resorted to using String manipulation & not a full fledged parser style code is to avoid the processing time within these event listeners as looking at the frequency at which the events get published we cannot have this whole processing consume a lot of time per event.
So wherever its imperative we use the xml parser utilities & where we can avoid we resorted to quick string manipulation.

I reckon we ovverlooked the xml declaration earlier & did not account that into the input while we were doing preliminary testing & carried out our initial testing without the xml declaration.
Now when we received the actual event xml we encountered this situtation.
So wanted to know a way of handling that xml declaration as part of the String.
Yea its always better to do this via xml parser way. Just reasoning out the approach we followed.


Thanks for all your responses
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change the regex pattern to:



I also added some stuff so that the regex will be more tolerant of extra whitespaces.

Henry
 
Mohit Sinha
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,

Thats awesome.Regex package seems to be quiet powerful.

Thanks Heaps.
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you producing this XML string that contains the XML declaration that you don't want? If so, then change whatever's producing the string to not produce that declaration.

If it's XSLT then <xsl utput omit-xml-declaration="yes"/>, for example.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mohit Sinha:
Hi Henry,

Thats awesome.Regex package seems to be quiet powerful.

Thanks Heaps.


Mohit,

I would highly recommend that you pick up a good book on Regular Expression. It is not a good idea to be responsible for (fixing or modifying) something that you have no idea how it works.

Henry
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!