Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Get data from XML formatted String

Posted on 2006-03-23
10
Medium Priority
?
278 Views
Last Modified: 2013-11-19
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
0
Comment
Question by:pcapplication
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 12

Accepted Solution

by:
Giant2 earned 336 total points
ID: 16278068
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
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 332 total points
ID: 16278578
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
 
LVL 8

Assisted Solution

by:Leo Eikelman
Leo Eikelman earned 332 total points
ID: 16279602
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:Leo Eikelman
ID: 16279618
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
 

Author Comment

by:pcapplication
ID: 16297402
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
 
LVL 8

Expert Comment

by:Leo Eikelman
ID: 16297555
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16297629
Do you need javax.swing in your code? If not, you can remove the import for it.
0
 
LVL 12

Expert Comment

by:Giant2
ID: 16319443
>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

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question