[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Reading a value from XML with namespaces

Posted on 2011-09-13
10
Medium Priority
?
374 Views
Last Modified: 2012-05-12
HI
I have a document with the following root :
<xxx:Envelope xxx:version="yyy">

How do I get the version attribute value from java code ?

Thanks
0
Comment
Question by:elimesika
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 19

Author Comment

by:elimesika
ID: 36533581
hi
Thanks for the quick respond , but I didn't found in your example how to get a value that is prefixed by a namespace (xxx:version)  
Please elaborate

Thanks
0
 
LVL 19

Author Comment

by:elimesika
ID: 36533587
also , I would like to get the value with XPATH and not by loping all nodes ....
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 47

Expert Comment

by:for_yan
ID: 36533626
Can you post an example of the actual file you want to read ?
0
 
LVL 19

Author Comment

by:elimesika
ID: 36533675
HI
Here it is
I want to get the ovf:version value

Thanks
<?xml version='1.0' encoding='UTF-8'?><ovf:Envelope xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1/" xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData" xmlns:vssd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ovf:version="1.0"><References><File ovf:href="f4448fd6-59ec-4af2-bc13-92cc87bcdb78/5ee9b198-ba30-44d2-8304-5abe331dc3ff" ovf:id="5ee9b198-ba30-44d2-8304-5abe331dc3ff" ovf:size="0" ovf:description="_Master_01/06/2010 17:36:05_template" /></References><Section xsi:type="ovf:NetworkSection_Type"><Info>List of networks</Info><Network ovf:name="Network 1" /></Section><Section xsi:type="ovf:DiskSection_Type"><Info>List of Virtual Disks</Info><Disk ovf:diskId="5ee9b198-ba30-44d2-8304-5abe331dc3ff" ovf:size="0" ovf:actual_size="0" ovf:vm_snapshot_id="dcd8f3a3-4f3b-4727-b2e6-b8e9ec5804d1" ovf:parentRef="" ovf:fileRef="f4448fd6-59ec-4af2-bc13-92cc87bcdb78/5ee9b198-ba30-44d2-8304-5abe331dc3ff" ovf:format="http://www.vmware.com/specifications/vmdk.html#sparse" ovf:volume-format="RAW" ovf:volume-type="Sparse" ovf:disk-interface="IDE" ovf:disk-type="System" ovf:boot="true" ovf:wipe-after-delete="false" /></Section><Content ovf:id="out" xsi:type="ovf:VirtualSystem_Type"><Name>Master</Name><TemplateId>a36a7ea2-d434-45cb-b32f-7a6c269c97ea</TemplateId><Description></Description><Domain></Domain><CreationDate>2010/06/01 02:36:00</CreationDate><IsAutoSuspend>false</IsAutoSuspend><TimeZone>Israel Standard Time</TimeZone><VmType>0</VmType><default_display_type>0</default_display_type><default_boot_sequence>0</default_boot_sequence><Section ovf:id="a36a7ea2-d434-45cb-b32f-7a6c269c97ea" ovf:required="false" xsi:type="ovf:OperatingSystemSection_Type"><Info>Guest Operating System</Info><Description>WindowsXP</Description></Section><Section xsi:type="ovf:VirtualHardwareSection_Type"><Info>1 CPU, 512 Memeory</Info><System><vssd:VirtualSystemType>XXXX 2.3.0.0</vssd:VirtualSystemType></System><Item><rasd:Caption>1 virtual cpu</rasd:Caption><rasd:Description>Number of virtual CPU</rasd:Description><rasd:InstanceId>1</rasd:InstanceId><rasd:ResourceType>3</rasd:ResourceType><rasd:num_of_sockets>1</rasd:num_of_sockets><rasd:cpu_per_socket>1</rasd:cpu_per_socket></Item><Item><rasd:Caption>512 MB of memory</rasd:Caption><rasd:Description>Memory Size</rasd:Description><rasd:InstanceId>2</rasd:InstanceId><rasd:ResourceType>4</rasd:ResourceType><rasd:AllocationUnits>MegaBytes</rasd:AllocationUnits><rasd:VirtualQuantity>512</rasd:VirtualQuantity></Item><Item><rasd:Caption>Drive 1</rasd:Caption><rasd:InstanceId>5ee9b198-ba30-44d2-8304-5abe331dc3ff</rasd:InstanceId><rasd:ResourceType>17</rasd:ResourceType><rasd:HostResource>f4448fd6-59ec-4af2-bc13-92cc87bcdb78/5ee9b198-ba30-44d2-8304-5abe331dc3ff</rasd:HostResource><rasd:Parent>00000000-0000-0000-0000-000000000000</rasd:Parent><rasd:Template>00000000-0000-0000-0000-000000000000</rasd:Template><rasd:ApplicationList></rasd:ApplicationList><rasd:StorageId>875bd4ea-88e2-440f-b5cb-2fc1efa3bb60</rasd:StorageId><rasd:StoragePoolId>106a6029-f6f0-4a5d-bb35-82d6b6448420</rasd:StoragePoolId><rasd:CreationDate>2010/06/01 11:25:50</rasd:CreationDate><rasd:LastModified>2010/06/01 11:25:50</rasd:LastModified></Item><Item><rasd:Caption>Ethernet adapter on xxxx</rasd:Caption><rasd:InstanceId>3</rasd:InstanceId><rasd:ResourceType>10</rasd:ResourceType><rasd:ResourceSubType>0</rasd:ResourceSubType><rasd:Connection>xxxx</rasd:Connection><rasd:Name>ddd</rasd:Name><rasd:speed>1000</rasd:speed></Item><Item><rasd:Caption>Ethernet adapter on xxxx</rasd:Caption><rasd:InstanceId>4</rasd:InstanceId><rasd:ResourceType>10</rasd:ResourceType><rasd:ResourceSubType>2</rasd:ResourceSubType><rasd:Connection>xxxx</rasd:Connection><rasd:Name>eth0</rasd:Name><rasd:speed>1000</rasd:speed></Item><Item><rasd:Caption>USB Controller</rasd:Caption><rasd:InstanceId>5</rasd:InstanceId><rasd:ResourceType>23</rasd:ResourceType><rasd:UsbPolicy>Enabled</rasd:UsbPolicy></Item><Item><rasd:Caption>Graphical Controller</rasd:Caption><rasd:InstanceId>6</rasd:InstanceId><rasd:ResourceType>20</rasd:ResourceType><rasd:VirtualQuantity>1</rasd:VirtualQuantity></Item></Section></Content></ovf:Envelope>

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36533723
Don't know if this is what you want:

    String xmlData1 =    "<?xml version='1.0' encoding='UTF-8'?><ovf:Envelope xmlns:ovf=\"http://schemas.dmtf.org/ovf/envelope/1/\" xmlns:rasd=\"http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData\" " +
                "xmlns:vssd=\"http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
                "ovf:version=\"1.0\"><References><File ovf:href=\"f4448fd6-59ec-4af2-bc13-92cc87bcdb78/5ee9b198-ba30-44d2-8304-5abe331dc3ff\" ovf:id=\"5ee9b198-ba30-44d2-8304-5abe331dc3ff\" " +
                "ovf:size=\"0\" ovf:description=\"_Master_01/06/2010 17:36:05_template\" /></References><Section xsi:type=\"ovf:NetworkSection_Type\">" +
                "<Info>List of networks</Info><Network ovf:name=\"Network 1\" /></Section><Section xsi:type=\"ovf:DiskSection_Type\"><Info>List of Virtual Disks</Info>" +
                "<Disk ovf:diskId=\"5ee9b198-ba30-44d2-8304-5abe331dc3ff\" ovf:size=\"0\" ovf:actual_size=\"0\" ovf:vm_snapshot_id=\"dcd8f3a3-4f3b-4727-b2e6-b8e9ec5804d1\" ovf:parentRef=\"\"" +
                " ovf:fileRef=\"f4448fd6-59ec-4af2-bc13-92cc87bcdb78/5ee9b198-ba30-44d2-8304-5abe331dc3ff\" ovf:format=\"http://www.vmware.com/specifications/vmdk.html#sparse\"" +
                " ovf:volume-format=\"RAW\" ovf:volume-type=\"Sparse\" ovf:disk-interface=\"IDE\" ovf:disk-type=\"System\" ovf:boot=\"true\" " +
                "ovf:wipe-after-delete=\"false\" /></Section><Content ovf:id=\"out\" xsi:type=\"ovf:VirtualSystem_Type\"><Name>Master</Name>" +
                "<TemplateId>a36a7ea2-d434-45cb-b32f-7a6c269c97ea</TemplateId><Description></Description><Domain>" +
                "</Domain><CreationDate>2010/06/01 02:36:00</CreationDate><IsAutoSuspend>false</IsAutoSuspend>" +
                "<TimeZone>Israel Standard Time</TimeZone><VmType>0</VmType><default_display_type>0</default_display_type>" +
                "<default_boot_sequence>0</default_boot_sequence><Section ovf:id=\"a36a7ea2-d434-45cb-b32f-7a6c269c97ea\" ovf:required=\"false\" xsi:type=\"ovf:OperatingSystemSection_Type\">" +
                "<Info>Guest Operating System</Info><Description>WindowsXP</Description>" +
                "</Section><Section xsi:type=\"ovf:VirtualHardwareSection_Type\">" +
                "<Info>1 CPU, 512 Memeory</Info><System><vssd:VirtualSystemType>XXXX 2.3.0.0</vssd:VirtualSystemType>" +
                "</System><Item><rasd:Caption>1 virtual cpu</rasd:Caption><rasd:Description>Number of virtual CPU</rasd:Description>" +
                "<rasd:InstanceId>1</rasd:InstanceId><rasd:ResourceType>3</rasd:ResourceType><rasd:num_of_sockets>1</rasd:num_of_sockets>" +
                "<rasd:cpu_per_socket>1</rasd:cpu_per_socket></Item><Item><rasd:Caption>512 MB of memory</rasd:Caption>" +
                "<rasd:Description>Memory Size</rasd:Description>" +
                "<rasd:InstanceId>2</rasd:InstanceId><rasd:ResourceType>4</rasd:ResourceType><rasd:AllocationUnits>MegaBytes</rasd:AllocationUnits>" +
                "<rasd:VirtualQuantity>512</rasd:VirtualQuantity></Item><Item><rasd:Caption>Drive 1</rasd:Caption>" +
                "<rasd:InstanceId>5ee9b198-ba30-44d2-8304-5abe331dc3ff</rasd:InstanceId><rasd:ResourceType>17</rasd:ResourceType>" +
                "<rasd:HostResource>f4448fd6-59ec-4af2-bc13-92cc87bcdb78/5ee9b198-ba30-44d2-8304-5abe331dc3ff</rasd:HostResource>" +
                "<rasd:Parent>00000000-0000-0000-0000-000000000000</rasd:Parent>" +
                "<rasd:Template>00000000-0000-0000-0000-000000000000</rasd:Template>" +
                "<rasd:ApplicationList></rasd:ApplicationList><rasd:StorageId>875bd4ea-88e2-440f-b5cb-2fc1efa3bb60</rasd:StorageId>" +
                "<rasd:StoragePoolId>106a6029-f6f0-4a5d-bb35-82d6b6448420</rasd:StoragePoolId>" +
                "<rasd:CreationDate>2010/06/01 11:25:50</rasd:CreationDate><rasd:LastModified>2010/06/01 11:25:50</rasd:LastModified>" +
                "</Item><Item><rasd:Caption>Ethernet adapter on xxxx</rasd:Caption><rasd:InstanceId>3</rasd:InstanceId>" +
                "<rasd:ResourceType>10</rasd:ResourceType><rasd:ResourceSubType>0</rasd:ResourceSubType><rasd:Connection>xxxx</rasd:Connection>" +
                "<rasd:Name>ddd</rasd:Name><rasd:speed>1000</rasd:speed></Item><Item><rasd:Caption>Ethernet adapter on xxxx</rasd:Caption><rasd:InstanceId>4</rasd:InstanceId><rasd:ResourceType>10</rasd:ResourceType><rasd:ResourceSubType>2</rasd:ResourceSubType>" +
                "<rasd:Connection>xxxx</rasd:Connection><rasd:Name>eth0</rasd:Name><rasd:speed>1000</rasd:speed></Item><Item><rasd:Caption>USB Controller</rasd:Caption><rasd:InstanceId>5</rasd:InstanceId><rasd:ResourceType>23</rasd:ResourceType><rasd:UsbPolicy>Enabled</rasd:UsbPolicy>" +
                "</Item><Item><rasd:Caption>Graphical Controller</rasd:Caption><rasd:InstanceId>6</rasd:InstanceId><rasd:ResourceType>20</rasd:ResourceType>" +
                "<rasd:VirtualQuantity>1</rasd:VirtualQuantity></Item></Section></Content></ovf:Envelope>";




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

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

        NodeList list = null;
         NodeList list1 = null;
        try {
            xPath.setNamespaceContext(new NamespaceContext() {
                public String getNamespaceURI(String prefix) {
                    return "http://schemas.dmtf.org/ovf/envelope/1/";
                }
                public String getPrefix(String namespaceURI) {
                    return "ovf";
                }
                public Iterator getPrefixes(String namespaceURI) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }
            });

   

             list = (NodeList) xPath.evaluate("//ovf:Envelope",  source, XPathConstants.NODESET);


        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }
        for (int i = 0; i < list.getLength(); i++) {
            NamedNodeMap atr =  list.item(i).getAttributes();
            for(int j=0; j<atr.getLength(); j++){
                System.out.println(atr.item(j).getNodeName());
                  System.out.println(atr.item(j).getNodeValue());
                  System.out.println(atr.item(j).getNamespaceURI());
            }
            
           // System.out.println(atr);
            
            System.out.println(list.item(i).getTextContent());
        }


        

Open in new window


Output:

ovf:version
1.0
http://schemas.dmtf.org/ovf/envelope/1/
xmlns:ovf
http://schemas.dmtf.org/ovf/envelope/1/
http://www.w3.org/2000/xmlns/
xmlns:rasd
http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData
http://www.w3.org/2000/xmlns/
xmlns:vssd
http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData
http://www.w3.org/2000/xmlns/
xmlns:xsi
http://www.w3.org/2001/XMLSchema-instance
http://www.w3.org/2000/xmlns/
List of networksList of Virtual DisksMastera36a7ea2-d434-45cb-b32f-7a6c269c97ea2010/06/01 02:36:00falseIsrael Standard Time000Guest Operating SystemWindowsXP1 CPU, 512 MemeoryXXXX 2.3.0.01 virtual cpuNumber of virtual CPU1311512 MB of memoryMemory Size24MegaBytes512Drive 15ee9b198-ba30-44d2-8304-5abe331dc3ff17f4448fd6-59ec-4af2-bc13-92cc87bcdb78/5ee9b198-ba30-44d2-8304-5abe331dc3ff00000000-0000-0000-0000-00000000000000000000-0000-0000-0000-000000000000875bd4ea-88e2-440f-b5cb-2fc1efa3bb60106a6029-f6f0-4a5d-bb35-82d6b64484202010/06/01 11:25:502010/06/01 11:25:50Ethernet adapter on xxxx3100xxxxddd1000Ethernet adapter on xxxx4102xxxxeth01000USB Controller523EnabledGraphical Controller6201

Open in new window


0
 
LVL 19

Author Comment

by:elimesika
ID: 36533731
HI

Thanks again

If I want the output  to be only "1.0" (the value of ovf:version in the document)
Is there a direct way to access this value in XPATH without looping all notes ?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36533741
    String xmlData1 =    "<?xml version='1.0' encoding='UTF-8'?><ovf:Envelope xmlns:ovf=\"http://schemas.dmtf.org/ovf/envelope/1/\" xmlns:rasd=\"http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData\" " +
                "xmlns:vssd=\"http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
                "ovf:version=\"1.0\"><References><File ovf:href=\"f4448fd6-59ec-4af2-bc13-92cc87bcdb78/5ee9b198-ba30-44d2-8304-5abe331dc3ff\" ovf:id=\"5ee9b198-ba30-44d2-8304-5abe331dc3ff\" " +
                "ovf:size=\"0\" ovf:description=\"_Master_01/06/2010 17:36:05_template\" /></References><Section xsi:type=\"ovf:NetworkSection_Type\">" +
                "<Info>List of networks</Info><Network ovf:name=\"Network 1\" /></Section><Section xsi:type=\"ovf:DiskSection_Type\"><Info>List of Virtual Disks</Info>" +
                "<Disk ovf:diskId=\"5ee9b198-ba30-44d2-8304-5abe331dc3ff\" ovf:size=\"0\" ovf:actual_size=\"0\" ovf:vm_snapshot_id=\"dcd8f3a3-4f3b-4727-b2e6-b8e9ec5804d1\" ovf:parentRef=\"\"" +
                " ovf:fileRef=\"f4448fd6-59ec-4af2-bc13-92cc87bcdb78/5ee9b198-ba30-44d2-8304-5abe331dc3ff\" ovf:format=\"http://www.vmware.com/specifications/vmdk.html#sparse\"" +
                " ovf:volume-format=\"RAW\" ovf:volume-type=\"Sparse\" ovf:disk-interface=\"IDE\" ovf:disk-type=\"System\" ovf:boot=\"true\" " +
                "ovf:wipe-after-delete=\"false\" /></Section><Content ovf:id=\"out\" xsi:type=\"ovf:VirtualSystem_Type\"><Name>Master</Name>" +
                "<TemplateId>a36a7ea2-d434-45cb-b32f-7a6c269c97ea</TemplateId><Description></Description><Domain>" +
                "</Domain><CreationDate>2010/06/01 02:36:00</CreationDate><IsAutoSuspend>false</IsAutoSuspend>" +
                "<TimeZone>Israel Standard Time</TimeZone><VmType>0</VmType><default_display_type>0</default_display_type>" +
                "<default_boot_sequence>0</default_boot_sequence><Section ovf:id=\"a36a7ea2-d434-45cb-b32f-7a6c269c97ea\" ovf:required=\"false\" xsi:type=\"ovf:OperatingSystemSection_Type\">" +
                "<Info>Guest Operating System</Info><Description>WindowsXP</Description>" +
                "</Section><Section xsi:type=\"ovf:VirtualHardwareSection_Type\">" +
                "<Info>1 CPU, 512 Memeory</Info><System><vssd:VirtualSystemType>XXXX 2.3.0.0</vssd:VirtualSystemType>" +
                "</System><Item><rasd:Caption>1 virtual cpu</rasd:Caption><rasd:Description>Number of virtual CPU</rasd:Description>" +
                "<rasd:InstanceId>1</rasd:InstanceId><rasd:ResourceType>3</rasd:ResourceType><rasd:num_of_sockets>1</rasd:num_of_sockets>" +
                "<rasd:cpu_per_socket>1</rasd:cpu_per_socket></Item><Item><rasd:Caption>512 MB of memory</rasd:Caption>" +
                "<rasd:Description>Memory Size</rasd:Description>" +
                "<rasd:InstanceId>2</rasd:InstanceId><rasd:ResourceType>4</rasd:ResourceType><rasd:AllocationUnits>MegaBytes</rasd:AllocationUnits>" +
                "<rasd:VirtualQuantity>512</rasd:VirtualQuantity></Item><Item><rasd:Caption>Drive 1</rasd:Caption>" +
                "<rasd:InstanceId>5ee9b198-ba30-44d2-8304-5abe331dc3ff</rasd:InstanceId><rasd:ResourceType>17</rasd:ResourceType>" +
                "<rasd:HostResource>f4448fd6-59ec-4af2-bc13-92cc87bcdb78/5ee9b198-ba30-44d2-8304-5abe331dc3ff</rasd:HostResource>" +
                "<rasd:Parent>00000000-0000-0000-0000-000000000000</rasd:Parent>" +
                "<rasd:Template>00000000-0000-0000-0000-000000000000</rasd:Template>" +
                "<rasd:ApplicationList></rasd:ApplicationList><rasd:StorageId>875bd4ea-88e2-440f-b5cb-2fc1efa3bb60</rasd:StorageId>" +
                "<rasd:StoragePoolId>106a6029-f6f0-4a5d-bb35-82d6b6448420</rasd:StoragePoolId>" +
                "<rasd:CreationDate>2010/06/01 11:25:50</rasd:CreationDate><rasd:LastModified>2010/06/01 11:25:50</rasd:LastModified>" +
                "</Item><Item><rasd:Caption>Ethernet adapter on xxxx</rasd:Caption><rasd:InstanceId>3</rasd:InstanceId>" +
                "<rasd:ResourceType>10</rasd:ResourceType><rasd:ResourceSubType>0</rasd:ResourceSubType><rasd:Connection>xxxx</rasd:Connection>" +
                "<rasd:Name>ddd</rasd:Name><rasd:speed>1000</rasd:speed></Item><Item><rasd:Caption>Ethernet adapter on xxxx</rasd:Caption><rasd:InstanceId>4</rasd:InstanceId><rasd:ResourceType>10</rasd:ResourceType><rasd:ResourceSubType>2</rasd:ResourceSubType>" +
                "<rasd:Connection>xxxx</rasd:Connection><rasd:Name>eth0</rasd:Name><rasd:speed>1000</rasd:speed></Item><Item><rasd:Caption>USB Controller</rasd:Caption><rasd:InstanceId>5</rasd:InstanceId><rasd:ResourceType>23</rasd:ResourceType><rasd:UsbPolicy>Enabled</rasd:UsbPolicy>" +
                "</Item><Item><rasd:Caption>Graphical Controller</rasd:Caption><rasd:InstanceId>6</rasd:InstanceId><rasd:ResourceType>20</rasd:ResourceType>" +
                "<rasd:VirtualQuantity>1</rasd:VirtualQuantity></Item></Section></Content></ovf:Envelope>";




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

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

        NodeList list = null;
         NodeList list1 = null;
        try {
            xPath.setNamespaceContext(new NamespaceContext() {
                public String getNamespaceURI(String prefix) {
                    return "http://schemas.dmtf.org/ovf/envelope/1/";
                }
                public String getPrefix(String namespaceURI) {
                    return "ovf";
                }
                public Iterator getPrefixes(String namespaceURI) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }
            });

       

             list = (NodeList) xPath.evaluate("//ovf:Envelope",  source, XPathConstants.NODESET);


        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }
        for (int i = 0; i < list.getLength(); i++) {
            NamedNodeMap atr =  list.item(i).getAttributes();
            for(int j=0; j<atr.getLength(); j++){
                if(atr.item(j).getNodeName().indexOf("version") > -1)
                {
                     System.out.println(atr.item(j).getNodeName());
                        System.out.println(atr.item(j).getNodeValue());
                }

       
            }
            
    
        }

Open in new window


Output:

ovf:version
1.0

Open in new window

0
 
LVL 36

Accepted Solution

by:
mccarl earned 2000 total points
ID: 36533853
> If I want the output  to be only "1.0" (the value of ovf:version in the document) Is there a direct way to access this value in XPATH without looping all notes ?

Yes. As above but change line 61 to the following...

String version = xPath.evaluate("/ovf:Envelope/@ovf:version",  source);


... and get rid of everything in the for loop (lines 68 to 81)
0
 
LVL 19

Author Closing Comment

by:elimesika
ID: 36533868
Thanks a lot !!!
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

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 …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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.
Suggested Courses

650 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