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

How to set an Attribute with DOM & ASP?

Status
Not open for further replies.

6181

Programmer
Joined
Jun 11, 2004
Messages
1
Location
NL
I'm using DOM and ASP to output an XML file, which should look like this:

<filmarchive>
<film film_id="">
<title>Donnie Darko</title>
</film>
</filmarchive>

I request the inputdata from a form:

strfilm_id = Request.Form( "film_id" )
strtitle = Request.Form( "title" )

I then do the following:

XMLnode = "<film>" & _
Chr(13) & Chr(10) & Chr(9) & _
"<film_id>" & strDatum & "</film_id>" & _
Chr(13) & Chr(10) & Chr(9) & _ "<titel>" & strtitel & "</titel>" & _
Chr(13) & Chr(10) & Chr(10) & Chr(10) & Chr(9) & _
"</film>" & _

The above code nicely puts each artikle below each other in the XML file.

However, 'film_id' is currently an element, and I want it to be an attibute of the <film> element. How should I do this?

Below is the code I use to generate the XML file:

Set rootContent = objXML.documentElement
Set objXML2 = Server.CreateObject("Microsoft.XMLDOM")
objXML2.loadXML(XMLnode)
Set rootNewNode = objXML2.documentElement

Set refNode = rootContent.firstChild
rootContent.insertBefore rootNewNode, refNode

Any help would be greatly appreciated!
 
XMLnode = "<film>" & _
Chr(13) & Chr(10) & Chr(9) & _
"<film_id>" & strDatum & "</film_id>" & _
Chr(13) & Chr(10) & Chr(9) & _ "<titel>" & strtitel & "</titel>" & _
Chr(13) & Chr(10) & Chr(10) & Chr(10) & Chr(9) & _
"</film>" & _
This is not best practice. If your data contains one of the XML pre-defined entities (ampersand, less-than symbol, greater-than symbol, single-quote, and double-quote), you'll have problems. A better way to do it would be:
Code:
Dim MyDoc as MSXML2.DomDocument40
Dim MyRootElem As MSXML2.IXMLDOMElement
Dim MyFilmArchive As MSXML2.IXMLDOMElement
Dim MyFilm As MSXML2.IXMLDOMElement
Dim MyFilmID As MSXML2.IXMLDOMAttribute
Dim MyTitle As MSXML2.IXMLDOMElement

Set MyDoc = New MSXML2.DomDocument40
Set MyRootElem = MyDoc.CreateElement("root")
Call MyDoc.AppendChild(MyRootElem)

' ====
' Repeat below for each film
' ====

' Create the filmarchive element and append it to the root
Set MyFilmArchive = MyDoc.CreateElement("filmarchive")
Call MyRootElem.AppendChild(MyFilmArchive)

' Create the film element and id attribute and 
' append it to the filmarchive element
Set MyFilm = MyDoc.CreateElement("film")
Set MyFilmID = MyDoc.CreateAttribute("film_id")
MyFilmID.Value = strDatum
MyFilm.Attributes.Add(MyFilmID)
MyFilmArchive.AppendChild(MyFilm)

' Create the title element and append it to the film element
Set MyTitle = MyDoc.CreateElement("title")
MyTitle.Value = strTitel
MyFilm.AppendChild(MyTitle)

' ====
' Save the document
' ====
MyDoc.Save("C:\somefile.xml")
You'll notice that the file probably doesn't have the tab characters in there. They are only for human readability -- All they really do is make your file larger and make the XML parser do more work when it reads it. If you're using Internet Explorer to view your files, at a minimum you should be using Notepad instead. Better yet would be one of the XML-specific editors (do a Google).

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top