Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML with VB6 (Newbee) 1

Status
Not open for further replies.

Periko

Programmer
Feb 24, 2003
134
0
0
BE
Hello,

I'm rather new in this. When I have different nodes, how can I position myself on the right place and read out the data? I'm messing around with elements, nodes and nodelists, but don't get anywhere......

-Job
-Right
-order
<supcode="xxx" procode="yyy"/>

How can I read out the supcode from right, not risking i'm taking them from left ?
Sorry, seems stupid however, but it's complete new for me.
I'm using the xml 6.0 in the references from vb6.
Loading the document is ok, but that's all...

Thx for any help...
Periko...
 
[1] The last tag is not. (Suppose it is [tt]<z supcode="xxx" procode="yyy" />[/tt] with two attributes.)

[2] Suppose you have the object instantiated.
[tt] dim xmldoc as new msxml2.domdocument60[/tt]
After loading...
[tt] set onode=xmldoc.selectsinglenode("Job/Right/order/z")[/tt]
You can display the attributes like this.
[tt] msgbox onode.getAttribute("supcode") & vbcrlf & onode.getAttribute("procode")[/tt]
That's about it.
 
Hello tsuji,

thx very much, this helps me a lot.
Just one problem. I declared the onode as IXMLDOMNode. is this correct ? Because i don't get the .getattribute on this sort of declaration. However, when I declare a variable as IXMLDOMElement, then I have the .getattribute.

You helped me a lot on my way to the solution, but this last step i'm missing. Can you help me out with this one, please ?

Kind regards...
Periko
 
>I declared the onode as IXMLDOMNode. is this correct ? Because i don't get the .getattribute on this sort of declaration. However, when I declare a variable as IXMLDOMElement, then I have the .getattribute.
Yes, you do need to declare it IXMLDOMElement.
[tt] dim onode as IXMLDOMElement[/tt]
I am used to taking a very liberal and economic approach to naming variables. If you do not have all the inflating types memorized, like me, you can always declare it variant and then debug.print typename of it and turn back to make thing more early-bind. But what you did is the right one.
 
I don't get it, my onode always gives my 'nothing' after done the selectsinglenode.

Does it matter that my xml-file looks as follows :

<?xml version="1.0" encoding="utf-8" ?>
<Job xmlns=" caclmode="xxx">
<Right>
<Order>
<SupCode="xxx" ProCode="yyy" />
</Order>
</Right>
</Job>
This is exactly like it is specified.

and my command is
Set VElem = XmlDoc.selectSingleNode("Job/Right/Order")
where the VElem is declared as IXMLDOMElement

After executing this velem-command, the VElem gives my Nothing, so the getattribute won't work too.

It looks all so simple, this little exercise... What the hell am i doing wrong?
 
><SupCode="xxx" ProCode="yyy" />
This is not an xml element. The whole document is not well-formed. Parsing it, xmldoc will pick up non-zero parseError's error code. You can monitor it (xmldoc.parseerror.errorcode).
 
Upon reading your xml source, you have now a default namespace defined. In that case, you need one more thing to do which is to defined "SelectionNamespaces" and using qualified name in the xpath. (I re-insert the symbolic z tag I used and change some case difference between your two posts.)
[tt]
'etc... (loading)
xmldoc.setProperty "SelectionNamespaces","xmlns:ns=''etc etc...
set VElem=XmlDoc.selectSingleNode("/ns:Job/ns:Right/ns:Order/ns:z")
msgbox VElem.getAttribute("SupCode") & vbcrlf & VElem.getAttribute("ProCode")
[/tt]
 
Hello,

Thx so far, this namespaces thing does it almost.

my Velem is not empty anymore.
But are you sure that this "SupCode" acts like an attribute ? Because when I call this with the msgbox line, I get an "invalid use of null --> rte94" on this line, like he get's nothing for the SupCode...

I understand what you try to say with this "z" thing, but it is not anywhere in this xml-document.

thx anyway, you deserve your stars... :)

Periko
 
HOLD ON, My Friend,

I found the solution, silly my !!!!!!

This "SupCode" was proceeded by a "Product", which is what you mean with the "z". Now I get a result in my msgbox, exactly what is in xml-file.

Many thx... You are great !!!!!!!!

Periko
 
Hello again tsuji,

just another question, I read out my complete document with displays on my text-boxes in vb.

When I change data in these text-boxes, how can I update my xml-document ? Do I have to create everything again followed with a save ? Or can I change the content of these attributes ?

Regards...
Periko...
 
You would have to load back the .text string which contains the complete source document.
[tt] XmlDoc.loadxml textboxcontrol.text[/tt]
(Be mindful about the encoding which may not be a problem.)
 
Or if you mean by "text-boxes" in pluriel each contains some desired value of some text node or some attribute node etc etc, then you would have to get the node object first and assign the value read from the "text-box". In any way it is possible but that is completely different problem that you should ask separately and concretely as there will be _no end_ in detail of them.
 
for once, I found it myself :)

I just use the velem.setattribute after being read first the attribute. Afterwards, a docxml.save(file location) and done.

That's the trick, not ?

Kind regards...
periko...
 
Hello Tsuji,

I did it the wrong way, now we have had a meeting.
I should not fill up an existing xml, but create a new one for each job I have to do.

So, question .... How do I create a namespace in vb-code ?

Can you help me on my way ?

Periko...
 
Hello Tsuji,

This helped me yet a lot, but nowhere is specified how to create this namespace...... This is the first problem I have to resolve, then I can continue to build up my xml-tree.

I tried with the createprocessinginstruction, but I have to create first my root tag. It's in this root-tag, my xmlns=" should come, so it should look like :

- <root xmlns=" attr1="xxx" attr2="yyy">
- ...
</root>

Can you give me the golden hint please, i cannot find anything regarding to namespaces in vb ....

Kind regards...
Periko...
 
Create the namespace declaration is not much different from setting up an attribute. You can discover the essential from this sketch.
[tt]
set oxmldoc=createobject("msxml2.domdocument.4.0") 'late bind version
set oroot=oparser.createElement("root") 'IXMLDOMElement
with oroot
.setAttribute "xml:xsd"," .setAttribute "xmlns:xsl"," .setAttribute "xmlns","="end with
set oxmldoc.documentElement=root
'etc etc and save
[/tt]
Discover the effect of the result. (I am going off for the day.)
 
Hell Tsuji,

I will check this one out, should solve my problem.

Thx, have a nice day...

Periko
 
>[self]set oroot=oparser.createElement("root") 'IXMLDOMElement
should obviously be read like this.
[tt]set oroot=[red]oxmldoc[/red].createElement("root") 'IXMLDOMElement[/tt]
(I last minute changed to have the variable showing xmldoc as people seem to be more happy with that notion but failed to change all occurences of it.)
 
Hello Tsuji,

The stuff with the creation of the namespace is clear !

However, one little problem is left. The first element (childnode) I append on the root-element is getting also sht like an empty namespace.... I show you what i did :

Set xNode = XD.createProcessingInstruction("xml", "version ='1.0' encoding = 'utf-8'")

XD.appendChild xNode
Set xNode = Nothing

Set rElem = XD.createElement("Root")
With rElem
.setAttribute "xmlns", " .setAttribute "aaa", "value1"
.setAttribute "bbb", "value2"
End With
Set XD.documentElement = rElem

Set xElem = XD.createElement("FirstChild")
rElem.appendChild xElem

Set xElem1 = XD.createElement("ChildOfFirstChild")
xElem.appendChild xElem1

And here is the result :

<?xml version="1.0" encoding="utf-8" ?>
- <Root xmlns=" aaa="value1" bbb="value2">
- <FirstChild xmlns="">
<ChildOfFirstChild />
</FirstChild>
</Root>

I don't understand why for my first child the xmlns="" is comming back, this may not be there for the xml i'm dressing up.

What am I doing wrong ?

Kind regads...
Periko
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top