excel - Getting XML Node value when Namespace is present using VBA -


i want navigate , details of xml document. has namespace. due namespace in multiblock element, getting lists.length zero.(please refer vba code below)

please guide. in advance reading. :) xml document is:

<?xml version="1.0" encoding="utf-16"?> <multiblock xmlns="x-schema:configfileschema.xml"> <erdbversion>     <dbversion>14.0</dbversion>     <dbdesc>erdb release exp431.1-49.0</dbdesc>     <dbguid>56cfaf87-53a9-4042-8b4f-4cf94868416e</dbguid>            <dblangid>enu</dblangid> </erdbversion> <block>     <blockdef>         <blockname>j60aov1136</blockname>         <entityname>j60aov1136</entityname>         <blockid>20031267</blockid>         <blockguid>d11bf0db-803d-49fc-a594-d234abd1e156              </blockguid>         <blockdesc>exported on (mm-dd-yy hh:mm) 07-31-              2017   10:12</blockdesc>         <templatename>system:controlmodule</templatename>                <classname>controlmodule</classname>                         <basetemplatename>system:controlmodule                       </basetemplatename>         <createtype> </createtype>         <attribute>1610613248</attribute>         <lifecyclestate>loaded</lifecyclestate>                  <assignedto>stfcee8a_03</assignedto>         <container></container>     </blockdef>     <parameters>         <parameter>             <paramname>aliasopt</paramname>             <paramvalue>off</paramvalue>         </parameter>         <parameter>             <paramname>discovorder</paramname>                       <paramvalue>"tpn"</paramvalue>         </parameter>         <parameter>             <paramname>methodscope</paramname>                       <paramvalue>"all"</paramvalue>         </parameter>     </parameters </block </multiblock> 

when trying node details without xmlns="x-schema:configfileschema.xml" able navigate through nodes , values.

but same document , xmlns attribute, having problem in navigating.

here vba code:

sub aovxml() dim xdoc msxml2.domdocument dim firstnamefield msxml2.ixmldomnodelist dim lists msxml2.ixmldomnodelist dim i, j integer 'dim lists msxml2.ixmldomnode  set xdoc = createobject("msxml2.domdocument") xdoc.async = false: xdoc.validateonparse = false xdoc.load (thisworkbook.path & "\j60aov1136.cnf.xml")  'reading list node set lists = xdoc.selectnodes("//multiblock/block") msgbox "length of lists nodes : " & lists.length & vbcrlf & _     "first xml list node : "   'getting first child node under lists set firstnamefield = lists(0).childnodes msgbox firstnamefield.length  'looping through xml nodes under list node = 0 firstnamefield.length - 1 msgbox firstnamefield(i).xml next  set xdoc = nothing end sub 

create schema file , name configfileschema.xml in same folder j60aov1136.cnf.xml

<?xml version="1.0" encoding="utf-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/xmlschema"> 

use following modified sample:

option explicit sub aovxml()     dim xdoc msxml2.domdocument60     dim firstnamefield msxml2.ixmldomnodelist     dim lists msxml2.ixmldomnodelist     dim integer     dim thepath string      set xdoc = createobject("msxml2.domdocument.6.0") 'must 6 schema parsing pass     xdoc.async = false     thepath = thisworkbook.path & "\j60aov1136.cnf.xml" '* '* useful have reason possible error '* sample code found here: '* https://msdn.microsoft.com/en-us/library/aa468547.aspx '*     if not xdoc.load(thepath) ' document failed load.         dim strerrtext string         dim xpe msxml2.ixmldomparseerror         set xpe = xdoc.parseerror         xpe         strerrtext = "your xml document failed load due following error." & vbcrlf & _             "error #: " & .errorcode & ": " & xpe.reason & _             "line #: " & .line & vbcrlf & _             "line position: " & .linepos & vbcrlf & _             "position in file: " & .filepos & vbcrlf & _             "source text: " & .srctext & vbcrlf & _             "document url: " & .url         end         msgbox strerrtext         exit sub     end if '* '* reading list node '* accept namespaces, use form //*[local-name()='<nodename>'] '*     set lists = xdoc.selectnodes("//*[local-name()='block']") '<- here major change     if lists.length > 0         msgbox "length of lists nodes : " & lists.length & vbcrlf & "first xml list node : " '* '* getting first child node under lists '*         set firstnamefield = lists(0).childnodes         msgbox firstnamefield.length '* '* looping through xml nodes under list node '*         = 0 firstnamefield.length - 1             msgbox firstnamefield(i).xml         next     end if     set xdoc = nothing end sub 

note need add reference microsoft xml, 6.0 use code.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -