This is Interesting: Free Magazines for Graphics designers and webmasters
Home > Archive > Microsoft XML > August 2006 > Adding a new node/element to an xml file
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Adding a new node/element to an xml file
|
|
| mattyp75@gmail.com 2006-08-22, 6:39 pm |
| I need to create an XML file that looks like the following:
<?xml version="1.0" encoding="UTF-8"?>
<Build Name="Test">
<Clusters Name="Test">
<Data Name="Test" />
<Cluster Name="A" Code="A" Num="1" R="" G="" B=""
ReportLevel="Group">
<Cluster Name="A1" Code="01" Num="1" ReportLevel="Type"/>
<Cluster Name="A2" Code="02" Num="2" ReportLevel="Type"/>
<Cluster Name="A3" Code="03" Num="3" ReportLevel="Type"/>
<Cluster Name="A4" Code="04" Num="4" ReportLevel="Type"/>
</Cluster>
<Cluster Name="B" Code="B" Num="1" R="" G="" B=""
ReportLevel="Group">
<Cluster Name="B5" Code="05" Num="5" ReportLevel="Type"/>
<Cluster Name="B6" Code="06" Num="6" ReportLevel="Type"/>
<Cluster Name="B7" Code="07" Num="7" ReportLevel="Type"/>
<Cluster Name="B8" Code="08" Num="8" ReportLevel="Type"/>
</Cluster>
</Clusters>
</Build>
I can easily create the first part down to the <Cluster Name="B", but i
can't seem to add in the extra node/element after the first Cluster
group tag has been closed off. I am writing my code in VB6.
I have created a second Cluster tag <Cluster\> but really need to
create <Cluster><\Cluster> and then be able to select this so that i
can append further elements to that.
Dim objNode As IXMLDOMNode
Dim xmlElem As IXMLDOMElement
..
.. 'there is code that reads from an Access database and i have code
that creates the lines
<?xml version="1.0" encoding="UTF-8"?>
<Build Name="Test">
<Clusters Name="Test">
<Data Name="Test" />
and then a loop through a database. After creating the top
<Cluster></Cluster> and all the information inside them i cannot seem
to select the second <Cluster/> so that i can start appending elements
to that.
Set xmlElem = xmlDoc.createElement("Cluster")
Set objNode = xmlDoc.selectSingleNode("//Build/Clusters")
objNode.appendChild xmlElem
Set objNode =
xmlDoc.selectSingleNode("//Build/Clusters/Cluster")
Maybe i can set up an IXMLNodeList and then move to the last node to
select it, but i've tried everything and looked on msdn and still can't
seem to get this working. I thought i might be able to use syntax such
as:
Set objNode = xmlDoc.selectSingleNode("//Build/Clusters/Cluster(2)")
to select the second <Cluster\>, but it doesn't like this syntax. If
anyone can help i'm fairly new to programming in DOM and am getting
confused between nodes, elements, items etc. etc. I saw that there was
an InsertBefore method, is there no way to insert after? If anyone can
help please let me know. Your help will be much appreciated,
Matt
| |
| Joe Fawcett 2006-08-23, 6:46 am |
| I think you need:
Set objNode = xmlDoc.selectSingleNode("/Build/Clusters/Cluster[2]")
If not try to be more precise about which cluster you are talking about, or
show your source data.
(It is poor practice, unfortunately exemplified by hundreds of examples
online, to start expressions with //.)
--
Joe Fawcett (MVP - XML)
http://joe.fawcett.name
<mattyp75@XXXXXXXXXX> wrote in message
news:1156269537.636549.279570@i42g2000cwa.googlegroups.com...
>I need to create an XML file that looks like the following:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <Build Name="Test">
> <Clusters Name="Test">
> <Data Name="Test" />
> <Cluster Name="A" Code="A" Num="1" R="" G="" B=""
> ReportLevel="Group">
> <Cluster Name="A1" Code="01" Num="1" ReportLevel="Type"/>
> <Cluster Name="A2" Code="02" Num="2" ReportLevel="Type"/>
> <Cluster Name="A3" Code="03" Num="3" ReportLevel="Type"/>
> <Cluster Name="A4" Code="04" Num="4" ReportLevel="Type"/>
> </Cluster>
> <Cluster Name="B" Code="B" Num="1" R="" G="" B=""
> ReportLevel="Group">
> <Cluster Name="B5" Code="05" Num="5" ReportLevel="Type"/>
> <Cluster Name="B6" Code="06" Num="6" ReportLevel="Type"/>
> <Cluster Name="B7" Code="07" Num="7" ReportLevel="Type"/>
> <Cluster Name="B8" Code="08" Num="8" ReportLevel="Type"/>
> </Cluster>
> </Clusters>
> </Build>
>
> I can easily create the first part down to the <Cluster Name="B", but i
> can't seem to add in the extra node/element after the first Cluster
> group tag has been closed off. I am writing my code in VB6.
>
> I have created a second Cluster tag <Cluster\> but really need to
> create <Cluster><\Cluster> and then be able to select this so that i
> can append further elements to that.
>
> Dim objNode As IXMLDOMNode
> Dim xmlElem As IXMLDOMElement
> .
> . 'there is code that reads from an Access database and i have code
> that creates the lines
>
> <?xml version="1.0" encoding="UTF-8"?>
> <Build Name="Test">
> <Clusters Name="Test">
> <Data Name="Test" />
>
> and then a loop through a database. After creating the top
> <Cluster></Cluster> and all the information inside them i cannot seem
> to select the second <Cluster/> so that i can start appending elements
> to that.
>
> Set xmlElem = xmlDoc.createElement("Cluster")
> Set objNode = xmlDoc.selectSingleNode("//Build/Clusters")
> objNode.appendChild xmlElem
>
> Set objNode =
> xmlDoc.selectSingleNode("//Build/Clusters/Cluster")
>
> Maybe i can set up an IXMLNodeList and then move to the last node to
> select it, but i've tried everything and looked on msdn and still can't
> seem to get this working. I thought i might be able to use syntax such
> as:
>
> Set objNode = xmlDoc.selectSingleNode("//Build/Clusters/Cluster(2)")
>
> to select the second <Cluster\>, but it doesn't like this syntax. If
> anyone can help i'm fairly new to programming in DOM and am getting
> confused between nodes, elements, items etc. etc. I saw that there was
> an InsertBefore method, is there no way to insert after? If anyone can
> help please let me know. Your help will be much appreciated,
> Matt
>
| |
| mattyp75@gmail.com 2006-08-23, 6:46 am |
| Thanks for your comments. What you have suggested does work, however i
have the code in a for next loop and need to set the value in the [] to
be the incremental value in the loop. I have tried
Set objNode = xmlDoc.selectSingleNode("/Build/Clusters/Cluster[i]")
where i is the incremental value, but it doesn't accept this code. Any
ideas as to how i can get around this?
| |
| mattyp75@gmail.com 2006-08-23, 6:46 am |
| I have managed to sort out the issue (i didn't at first notice it was
in quotes. The following line works.
Set objNode = xmlDoc.selectSingleNode("/Build/Clusters/Cluster[" & i &
"]")
Many thanks for your help,
Matt
|
|
|
| | Copyright 2003 - 2008 forum4designers.com Software forum Computer Hardware reviews |
|