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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modifying attributes via VBA

Status
Not open for further replies.

RickBeddoe

Programmer
Nov 18, 2008
32
US
I am trying to edit an attribute of a XML file using VBA. Here is my code so far:

Dim XMLDOC As DOMDocument
Set XMLDOC = New DOMDocument
Dim myNodes As IXMLDOMNodeList
Dim Node As IXMLDOMNode
Dim attrs As IXMLDOMNamedNodeMap
Dim attr As IXMLDOMAttribute

XMLDOC.Load ("ci.xsd")



Set Node = XMLDOC.selectSingleNode("xsd:schema")

Set attrs = Node.Attributes
Set attr = attrs.getNamedItem("xmlns:ci")
attr.Text = "foo"

The last line generates an error telling me the node is read-only. I have been doing this in C# without a problem and I could make a dll to use in this VBA, but I'm trying to make this a very simple app for someone else to follow in case I get 'downsized'. Is it possible to edit the attribute value using VBA?

TIA

 
[1] I can offer a "downsized" vbscript solution. It is easily "upsided" to vba first, then to early binding, second.
[tt]
const adModeReadWrite=3
const adTypeBinary=1
const adTypeText=2
const adSaveCreateOverWrite=2
const adReadAll=-1
const adWriteChar=0

dim s,t,v
dim xmlfile,outfile,suri,suri_new
dim ostream,rx
[blue]
'user's givens [a]
xmlfile="d:\xyz\abc.xsd"
outfile="d:\pqr\abc_revised.xsd")

'user's givens
suri="[ignore][/ignore]"
suri_new="foo"
[/blue]
set ostream=createobject("adodb.stream")
with ostream
.mode=adModeReadWrite
.type=adTypeText
.open
.charset="utf-8"
.loadfromfile xmlfile
.position=0
s=.readtext(adReadAll)
.close
end with

set rx=new regexp
with rx
.global=true
'specific care taken for degree of freedom in xml structure (non-canonical)
.pattern="(\s*=\s*('|"")\s*)" & suri & "(\s*\2)" end with
t=rx.replace(s,"$1" & suri_new & "$3")

with ostream
.open
.charset="utf-8"
.writetext t,adWriteChar
.position=0
.type=adTypeBinary
.position=3 'utf-8 bom takes up 3 bytes
v=.read
.close
end with

with ostream
.open
.type=adTypeBinary
.write v
.savetofile outfile,adSaveCreateOverWrite
.close
end with
set ostream=nothing
[/tt]
[2] Many points are implemented with care to take care of xml's structure-specific and also the avoidance of bom of physical file saving. Amongst those specifics, I just want to mention above the internationalization perspective of xml usage. That is taken as one of the core motivation of using adodb.stream.

[3] The above should take care of more than xmlns:ci. The attributes (name/value pair) involving the mentioned url change could appear in more than that particular structure, such as targetNamespace attribute or default attribute or namespace attribute in tags. The pattern built in the script could change them all accordingly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top