Get data from XML formatted String

Hello,

I have a problem like this. There are 2 application(let's say app1 and app2) that needs to communicate each other via Web Services and sending data in XML formatted string, not XML file. For example, app1 calling a web service provided by app2 and send this string :

<NewDataSet><xs:schema id="NewDataSet" xmlns=""
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="RCV">
<xs:complexType>
<xs:sequence>
<xs:element name="MCMPNO" type="xs:string" minOccurs="0"/>
<xs:element name="MBATCH" type="xs:string" minOccurs="0"/>
<xs:element name="MRECNO6" type="xs:string" minOccurs="0"/>
<xs:element name="MPGMID" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

<RCV>
<MCMPNO>401</MCMPNO>
<MBATCH>000000003000005</MBATCH>
<MRECNO6>000001</MRECNO6>
<MPGMID>POIN00D</MPGMID>
</RCV>
<RCV>
<MCMPNO>401</MCMPNO>
<MBATCH>000000203000005</MBATCH>
<MRECNO6>000002</MRECNO6>
<MPGMID>POIN00D</MPGMID>
</RCV>

</NewDataSet>

I need to get the "data only" from that string, that is : 401000000003000005000000POIN00D401000000203000005000002POIN00D

How to do that in JAVA ? Anyone hear can help ?

Thank's

Sudedy
pcapplicationAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Giant2Commented:
Here you can find many example about managing XML document:
http://javaalmanac.com/egs/org.w3c.dom/pkg.html
Hope this could help.
Bye, Giant.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mayank SAssociate Director - Product EngineeringCommented:
Load it into an XML Document and get the root-node using getDocumentElement (). Scroll to the RCV node-list and get it in a NodeList using the getElementsByTagName () method on the root-node. Iterate through the list and use getChildNodes ().item ( i ).getChildNodes ().item ( 0 ).getNodeValue () to get the text inside. Keep appending it to a StringBuffer.
0
Leo EikelmanDirector, IT and Business DevelopmentCommented:
You can do something like this

            String resultText = "";

            javax.xml.parsers.DocumentBuilder parser = null;
           
            Document doc = null;
           
            // result is the XML string you are going to parse
            // ie.
            StringReader adapteeReader = new StringReader(result);
            InputSource input = new InputSource(adapteeReader);
            javax.xml.parsers.DocumentBuilderFactory dfactory =
             javax.xml.parsers.DocumentBuilderFactory.newInstance();
            dfactory.setNamespaceAware(true);
            parser = dfactory.newDocumentBuilder();
            doc = parser.parse(input);
           
            NodeList children = doc.getFirstChild().getChildNodes();
           
            for (int x = 0; x < children.getLength(); x++)
            {
                Node child = children.item(x);
               
                NodeList attributes =  child.getChildNodes();
                for (int y = 0; y < attributes.getLength(); y++)
                {
                    Node attribute = attributes.item(y);
                    resultText = resultText + attribute.getFirstChild().getNodeValue();
                }
            }

this is only a snippet of code but it should get you started.  You might have to tweak it a little but it will be a good start.

Hope that helps : )

Leo
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Leo EikelmanDirector, IT and Business DevelopmentCommented:
instead of  resultText = resultText + attribute.getFirstChild().getNodeValue() you can use a StringBuffer

you can declare at the top

StringBuffer resultText = new StringBuffer()  

instead of

String resultText = "" 

and use


resultText.append(attribute.getFirstChild().getNodeValue());

instead of

resultText = resultText + attribute.getFirstChild().getNodeValue();


cheers,

Leo
0
pcapplicationAuthor Commented:
Dear Leo,

at this line,
 StringReader adapteeReader = new StringReader(result);

do you mean "result" is the "resultText "?

And, I get this message :

Type mismatch: cannot convert from org.w3c.dom.Document to javax.swing.text.Document

0
Leo EikelmanDirector, IT and Business DevelopmentCommented:
Yeah sorry, the line

StringReader adapteeReader = new StringReader(result);

Is supposed to be

StringReader adapteeReader = new StringReader(resultText);

hmm ok that is odd.  you are expecting a Document from the javax.swing and not a DOM document.

Which lineis causing the mismatch error?

Leo
0
Mayank SAssociate Director - Product EngineeringCommented:
Do you need javax.swing in your code? If not, you can remove the import for it.
0
Giant2Commented:
>javax.swing.text.Document
>org.w3c.dom.Document
Maybe you try to cast between these two. The cast could be caused by something like:
(javax.swing.text.Document)ddddd
or
(Document)ddddd

in first case remove the javax.swing.text.
in the second case following what mayankeagle told remove import javax.swing.text....

Bye, Giant.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.