XML Related API!

Posted on 2011-10-05
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
    LVL 47

    Assisted Solution


    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

    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

    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!

    LVL 86

    Assisted Solution

    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

    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.util.Iterator;
    public class forXPath {
        public static void main(String[] args) {
            String xmlData1 = "<LoginResponse xmlns=\"\">  " +
                       " <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 "";
                            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

    Thanks again for all your prompt and clear response!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    If you have upgraded to Java2 update 10 on a Microsoft Windows client, you may have discovered that your Java application does not work as it did before.  For example, the colors of your Java2D graphic may be all wrong for no apparent reason. Aft…
    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    794 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now