We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Get data from XML formatted String

Medium Priority
477 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
Comment
Watch Question

Commented:
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mayank SPrincipal Technologist
CERTIFIED EXPERT
Commented:
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.
Leo EikelmanDirector, IT and Business Development
Commented:
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
Leo EikelmanDirector, IT and Business Development

Commented:
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

Author

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

Leo EikelmanDirector, IT and Business Development

Commented:
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
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Do you need javax.swing in your code? If not, you can remove the import for it.

Commented:
>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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.