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


XML Related API!

Posted on 2011-10-05
Medium Priority
Last Modified: 2012-05-12
I'm sending a request to the server which inreturn sends back a series of configuration parameters in XML format.  I have read the server responce storing it into a String variable.  Now I'd like to read through elements and altributes in order to pick the desired settings and store them in my Java DB.  What would be the API that would perform simple XML related tasks?!

Would I have to store the XML file sent by the server as a file or it's already doo that it's stored as a String?!

Question by:snajalm
  • 3
  • 2
LVL 47

Assisted Solution

for_yan earned 1200 total points
ID: 36922467

read here about options:

DOM or SAX would be the most well known

SAX requires less memory (in case it matters for big files)

LVL 47

Assisted Solution

for_yan earned 1200 total points
ID: 36922472
In case the file is small, I don't think you need to write it to file - you can
make some ByteArrayInputStream out of the XML string - didn't try it myself, but
I'm sure there are requirted methods which would read from input stream rather than ffile

Author Comment

ID: 36922894
Thanks for you help so far!

So if we have an InputStream that is reading XML data off the URL connection.  How can I read this stream so that I pick an specific attribute occuring in different elements and store their values into a String array of some sort.

An example would be greatly appreciated!

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 86

Assisted Solution

CEHJ earned 400 total points
ID: 36922905
XPath is a good candidate. Try it on a file first while you're testing and i'll show you how to do that with a stream when you've got it working

LVL 47

Accepted Solution

for_yan earned 1200 total points
ID: 36923004
This is a rather simple example of XPath searching based on a String:

from this solution:

import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.xml.namespace.NamespaceContext;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.StringReader;
import java.util.Iterator;

public class forXPath {
    public static void main(String[] args) {

        String xmlData1 = "<LoginResponse xmlns=\"http://c1.net.corbis.com/\">  " +
                   " <sessionUID>{f7be62e6-0bf5-44a7-bf77-5ab2bf307a23}</sessionUID>   " +
                     "<eNewSTok>JdKVwENgF7SbdBX2x4R+BTA/WiJatMpCJvFckhNtzbx+WZ8OqmSu+fzD36XL4irDsbp69O8YioZl6iYcwrui6NWo6dBh7YCf18A4c4Ry3nFWLpBkUt35sQmBcON1kD79+1lvdJNZrzKOQIDo3Qs/ogb95aVrp7TAgjIkugti3Q0=</eNewSTok>    " +
                   " <securityTokenXML><![CDATA[<SecurityToken><ActualMemberUID Scope=\"Public\" Type=\"Guid\" Value=\"{b7fde077-c13e-40a7-aae9-f18c5a66f3e1}\"/><EffectiveMemberUID Scope=\"Public\" Type=\"Guid\" Value=\"{b7fde077-c13e-40a7-aae9-f18c5a66f3e1}\"/><RoleID Scope=\"Public\" Type=\"Long\" Value=\"49\"/><CountryCode Scope=\"Public\" Type=\"String\" Value=\"US\"/><RegionCode Scope=\"Public\" Type=\"String\" Value=\"CA\"/><LanguageID Scope=\"Public\" Type=\"Long\" Value=\"100958\"/><OnyxSessionID Scope=\"Public\" Type=\"String\" Value=\"\"/></SecurityToken>]]></securityTokenXML>   " +

                InputSource source = new InputSource(new StringReader(xmlData1));

                XPath xPath = XPathFactory.newInstance().newXPath();

                NodeList list = null;
                try {
                    xPath.setNamespaceContext(new NamespaceContext() {
                        public String getNamespaceURI(String prefix) {
                            return "http://c1.net.corbis.com/";
                        public String getPrefix(String namespaceURI) {
                            return "ns";
                        public Iterator getPrefixes(String namespaceURI) {
                            throw new UnsupportedOperationException("Not supported yet.");

                   // list = (NodeList) xPath.evaluate("(//ns:URL128/@Value)|(//ns:ImageUID/@Value)", source, XPathConstants.NODESET);

                     list = (NodeList) xPath.evaluate("(//ns:sessionUID)|(//ns:eNewSTok)",  source, XPathConstants.NODESET);

                } catch (Exception ex) {
                for (int i = 0; i < list.getLength(); i++) {



Open in new window


Open in new window


Author Closing Comment

ID: 36928653
Thanks again for all your prompt and clear response!

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Don’ts and Dos are two important end products of software testing basics that a tester needs to regard. This article attempts to explain the principles of both.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month10 days, 20 hours left to enroll

572 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