Thursday 20 February 2014

Reading XML

package com.rest;

import java.io.File;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class ReadRPCResponseXML {
    public static void main(String... str) {
        readXML("C:\\IBM\\WCDE_ENT70\\workspace\\RestTest\\req-response\\rpc-request.xml");
    }

    static Map readXML(String xml) {
        Map<String,List> specs = new HashMap<String,List>();
        try {
            File fXmlFile = new File(xml);
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            Document doc = dBuilder.parse(is);

            doc.getDocumentElement().normalize();
            System.out.println("Root element :"+ doc.getDocumentElement().getNodeName());
            NodeList nList = doc.getElementsByTagName("Specifications");
            System.out.println("Total ::: "+nList.getLength());
            List<String> spec = null;
            for (int temp = 0; temp < nList.getLength(); temp++) {
                spec = new ArrayList<String>();
                Node nNode = nList.item(temp);
                NodeList list = nNode.getChildNodes();
              
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
                    String publicname = eElement.getAttribute("publicname");
                    if("TECHSPEC".equals(publicname) || "FEATURES".equals(publicname) || "OVERVIEW".equals(publicname)){
                        System.out.println("Specifications : "+ eElement.getAttribute("publicname"));
                        System.out.println("----------------------------"+list.getLength());
                        for (int temp1 = 0; temp1 < list.getLength(); temp1++) {
                            Node nNode1 = list.item(temp1);
                            if (nNode1.getNodeType() == Node.ELEMENT_NODE) {
                                Element eElement1 = (Element) nNode1;
                                System.out.println(eElement1.getAttribute("label")+"-->"+eElement1.getElementsByTagName("Value").item(0).getChildNodes().item(0).getNodeValue().trim());
                                spec.add(eElement1.getAttribute("label")+" : "+eElement1.getElementsByTagName("Value").item(0).getChildNodes().item(0).getNodeValue().trim());
                            }
                        }
                        specs.put(eElement.getAttribute("publicname"), spec);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
      
        return specs;
      
    }
}

/*
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
    <queryResponse xmlns="http://service.asl.wsrpc.hp.com">
        <RichContent>
            <Specifications groupdisplayorder="0" publicname="ENLARGEDIMAGE"
                xmlns="http://common.wsrpc.hp.com">
                <Specification countrycode="uk" displayorder="1"
                    label="Photo (product photo, jpg, 400x400)" languagecode="en"
                    specgroupdisplayname="" specgroupdisplayorder="1"
                    xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">http://h10010.www1.hp.com/wwpc/images/emea/hp-z400-workstation_400x400.jpg
                    </Value>
                </Specification>
            </Specifications>
            <Specifications groupdisplayorder="0" publicname="FEATURES"
                xmlns="http://common.wsrpc.hp.com">
                <Specification countrycode="uk" displayorder="51"
                    label="AP operation modes" languagecode="en" specgroupdisplayname=""
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">&lt;p /></Value>
                </Specification>
                <Specification countrycode="uk" displayorder="1"
                    label="Features" languagecode="en" specgroupdisplayname=""
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="GH" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Features</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="52"
                    label="Footnote [01]" languagecode="en" specgroupdisplayname=""
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">&lt;span class="small">&lt;i>&lt;sup>1&lt;/sup>
                        Quad-Core is designed to improve performance of multithreaded
                        software products and hardware-aware multitasking operating
                        systems and may require appropriate operating system software for
                        full benefits; check with software provider to determine
                        suitability; not all customers or software applications will
                        necessarily benefit from use of these technologies. 64-bit
                        computing on Intel architecture requires a computer system with a
                        processor, chipset, BIOS, operating system, device drivers and
                        applications enabled for Intel® 64 architecture. Processors will
                        not operate (including 32-bit operation) without an Intel 64
                        architecture-enabled BIOS. Performance will vary depending on
                        your hardware and software configurations. See
                        www.intel.com/info/em64t for more information&lt;/i>&lt;/span>
                    </Value>
                </Specification>
                <Specification countrycode="uk" displayorder="4"
                    label="Key selling point" languagecode="en" specgroupdisplayname=""
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="SB" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Work smarter: run more tasks, processes and
                        applications simultaneously with a workstation that brings
                        together and optimizes the latest processors, memory, graphics
                        and storage technologies.</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="11"
                    label="Key selling point" languagecode="en" specgroupdisplayname=""
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="SB" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Updated design: with a smart new chassis, quiet
                        acoustics and optional liquid cooling that makes the system even
                        quieter at heavy loads, the HP Z400 showcases HP
                        innovation—inside and out.</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="18"
                    label="Key selling point" languagecode="en" specgroupdisplayname=""
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="SB" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Designed with the environment in mind: minimize
                        power and cooling costs with ENERGY STAR® qualified systems
                        featuring 85% efficient power supplies and an HP-developed
                        energy-saving feature that helps lower energy consumption in off
                        mode.</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="25"
                    label="Key selling point" languagecode="en" specgroupdisplayname=""
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="SB" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Do more every minute: take on bigger challenges
                        with the new Intel® QuickPath Interconnect architecture and your
                        choice of the latest dual- or quad-core Intel® Xeon® processors
                        &lt;sup>1&lt;/sup>.</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="32"
                    label="Key selling point" languagecode="en" specgroupdisplayname=""
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="SB" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Latest graphics: drive four 3D or six 2D displays
                        with the newest professional graphics options from ATI and
                        NVIDIA.</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="39"
                    label="Key selling point" languagecode="en" specgroupdisplayname=""
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="SB" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Simple to service: the tool-less chassis and
                        uncluttered internal design simplify access and service for
                        adding or changing components quickly.</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="2"
                    label="Key selling point MDA" languagecode="en"
                    specgroupdisplayname="" specgroupdisplayorder="1"
                    xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="SH" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Windows 8 Pro or other operating systems
                        available&lt;p /></Value>
                </Specification>
            </Specifications>
            <Specifications groupdisplayorder="0" publicname="LARGEIMAGE"
                xmlns="http://common.wsrpc.hp.com">
                <Specification countrycode="uk" displayorder="1"
                    label="Photo (product photo, jpg, 190x170)" languagecode="en"
                    specgroupdisplayname="" specgroupdisplayorder="1"
                    xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">http://h10010.www1.hp.com/wwpc/images/emea/hp-z400-workstation_190x170.jpg
                    </Value>
                </Specification>
            </Specifications>
            <Specifications groupdisplayorder="0" publicname="OVERVIEW"
                xmlns="http://common.wsrpc.hp.com">
                <Specification countrycode="uk" displayorder="1"
                    label="Overview" languagecode="en" specgroupdisplayname=""
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="GH" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Overview</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="2"
                    label="Overview" languagecode="en" specgroupdisplayname=""
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">With its revolutionary new architecture and bold
                        new industrial design, the HP Z400 Workstation helps you
                        accomplish more with every minute of your time and every dollar
                        of your investment.</Value>
                </Specification>
            </Specifications>
            <Specifications groupdisplayorder="0" publicname="TAGLINE"
                xmlns="http://common.wsrpc.hp.com">
                <Specification countrycode="uk" displayorder="1"
                    label="Legal tagline" languagecode="en" specgroupdisplayname=""
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">HP recommends Windows.</Value>
                </Specification>
            </Specifications>
            <Specifications groupdisplayorder="0" publicname="TECHSPEC"
                xmlns="http://common.wsrpc.hp.com">
                <Specification countrycode="uk" displayorder="2"
                    label="Audio" languagecode="en" specgroupdisplayname="Communications"
                    specgroupdisplayorder="6" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Integrated High Definition Realtek ALC262 Audio,
                        optional Creative X-Fi Titanium PCIe Audio Card, optional HP Thin
                        USB Powered Speakers</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="4"
                    label="Chipset" languagecode="en" specgroupdisplayname="System features"
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Intel® X58 Express</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="1"
                    label="Display" languagecode="en" specgroupdisplayname="Graphics"
                    specgroupdisplayorder="4" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">HP LP1965 19-inch LCD Monitor, HP LP2065 20-inch
                        LCD Monitor, HP LP2275w 22-inch Widescreen LCD Monitor, HP
                        LP2475w 24-inch Widescreen LCD Monitor, HP DreamColor LP2480zx
                        Professional Display (24-inch widescreen), HP LP3065 30-inch
                        Widescreen LCD Monitor (all sold separately)</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="1"
                    label="Energy efficiency" languagecode="en" specgroupdisplayname="Power"
                    specgroupdisplayorder="7" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">ENERGY STAR®, EPEAT Goldâ„¢, 80PLUS Bronze Power
                        Supply</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="2"
                    label="External drive bays" languagecode="en"
                    specgroupdisplayname="Storage" specgroupdisplayorder="3"
                    xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">3 external 5.25" bays</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="6"
                    label="Form factor" languagecode="en" specgroupdisplayname="System features"
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Convertible minitower</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="2"
                    label="Graphics" languagecode="en" specgroupdisplayname="Graphics"
                    specgroupdisplayorder="4" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">No integrated Graphic Card</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="5"
                    label="Hard drive" languagecode="en" specgroupdisplayname="Storage"
                    specgroupdisplayorder="3" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">500 GB 7200 rpm SATA NCQ</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="1"
                    label="Internal drive bays" languagecode="en"
                    specgroupdisplayname="Storage" specgroupdisplayorder="3"
                    xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">2 internal 3.5" bays (up to 4 with converter)
                    </Value>
                </Specification>
                <Specification countrycode="uk" displayorder="2"
                    label="Memory slots" languagecode="en" specgroupdisplayname="Memory"
                    specgroupdisplayorder="2" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">6 DIMM slots</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="1"
                    label="Memory, standard" languagecode="en" specgroupdisplayname="Memory"
                    specgroupdisplayorder="2" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">4 GB 1333 MHz DDR3 ECC Unbuffered RAM </Value>
                </Specification>
                <Specification countrycode="uk" displayorder="1"
                    label="Minimum dimensions (W x D x H)" languagecode="en"
                    specgroupdisplayname="Dimensions and weight"
                    specgroupdisplayorder="8" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">45.53 x 45.02 x 16.79 cm</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="1"
                    label="Network interface" languagecode="en" specgroupdisplayname="Communications"
                    specgroupdisplayorder="6" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Integrated Broadcom 5764 10/100/1000 PCIe LAN,
                        optional Broadcom NIC, optional Intel NIC</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="1"
                    label="Operating system" languagecode="en" specgroupdisplayname="System features"
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Windows XP Professional 32-bit (available through
                        downgrade rights from Genuine Windows® 7 Professional)</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="8"
                    label="Optical drive" languagecode="en" specgroupdisplayname="Storage"
                    specgroupdisplayorder="3" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">SATA 16X DVD+/-RW SuperMulti LightScribe</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="1"
                    label="Ports" languagecode="en" specgroupdisplayname="Expansion features"
                    specgroupdisplayorder="5" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">12 USB 2.0&lt;br> 1 IEEE 1394a&lt;br> 1 audio
                        in&lt;br> 2 audio out&lt;br> 2 microphone in&lt;br> 2 PS/2&lt;br>
                        1 RJ-45&lt;br/> 2 USB 3.0 (via optional PCIe card); 1 serial
                        (optional), 2 IEEE 1394a or 1394b (via optional PCI card); 4
                        internal USB 2.0 available by 2 separate 2x5 headers</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="2"
                    label="Power" languagecode="en" specgroupdisplayname="Power"
                    specgroupdisplayorder="7" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">475 watts wide-ranging, active Power Factor
                        Correction, 85% efficient Power Supply</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="3"
                    label="Processor" languagecode="en" specgroupdisplayname="System features"
                    specgroupdisplayorder="1" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">1 Intel® Xeon® Quad-Core Processor W3520 (2.66
                        GHz, 8 MB cache, 1066 MHz memory)</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="2"
                    label="Security management" languagecode="en"
                    specgroupdisplayname="What's included" specgroupdisplayorder="9"
                    xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">HP Solenoid Hood Lock/Sensor Kit, Kensington
                        Security Cable &amp; Lock</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="2"
                    label="Slots" languagecode="en" specgroupdisplayname="Expansion features"
                    specgroupdisplayorder="5" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">2 PCI Express Gen2 x16&lt;br> 1 PCI Express Gen2
                        (x8 mechanically, x4 electrically)&lt;br> 1 PCI Express Gen1 (x8
                        mechanically, x4 electrically)&lt;br> 2 PCI&lt;br> 1 22-in-1
                        Media Card Reader (optional)&lt;br/> The PCIe x8 connectors are
                        open ended, allowing a PCIe x16 card to be seated in the slot.
                    </Value>
                </Specification>
                <Specification countrycode="uk" displayorder="7"
                    label="Storage controller" languagecode="en" specgroupdisplayname="Storage"
                    specgroupdisplayorder="3" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Integrated SATA 3.0 Gb/s with RAID 0, 1, 10, 5
                        support. LSI 8888ELP 8-port SAS RAID (optional); LSI MegaRAID
                        9260-8i SAS 6Gb/s ROC RAID and iBBU08 Battery Backup Unit;
                    </Value>
                </Specification>
                <Specification countrycode="uk" displayorder="3"
                    label="Warranty" languagecode="en" specgroupdisplayname="What's included"
                    specgroupdisplayorder="9" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">3-year worldwide parts, labour and next business
                        day onsite service&lt;br> Terms and conditions may vary,
                        restrictions apply</Value>
                </Specification>
                <Specification countrycode="uk" displayorder="2"
                    label="Weight" languagecode="en" specgroupdisplayname="Dimensions and weight"
                    specgroupdisplayorder="8" xmlns="http://common.wsrpc.hp.com">
                    <Value displayformat="PT" height="0" oid="4080932" sku="KK581ET"
                        width="0" xmlns="http://common.wsrpc.hp.com">Starting at 13.5 kg</Value>
                </Specification>
            </Specifications>
            <ImageUrls xmlns="http://common.wsrpc.hp.com">
                <ImageUrl oid="4080932" sku="KK581ET" xmlns="http://common.wsrpc.hp.com">http://h10010.www1.hp.com/wwpc/images/emea/hp-z400-workstation_70x100.jpg
                </ImageUrl>
            </ImageUrls>
        </RichContent>
    </queryResponse>
</soap:Body>
</soap:Envelope>
*/

No comments:

Post a Comment