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

asp and xml

Status
Not open for further replies.

brakkie2

Programmer
Jul 19, 2004
6
ZA
Ok this is my asp code so far

<%
Response.Buffer = True
Dim objXMLHTTP, xml

' Create an xmlhttp object:
Set xml = Server.CreateObject("Microsoft.XMLHTTP")

key = Request.QueryString("key")

' Opens the connection to the remote server.
xml.Open "GET", " "& key &"" , False

' Sends the request and returns the data:
xml.Send

xmltext = xml.responseText

Set xml = Nothing

set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load(xmltext)

%>
Now how do I access individual nodes from xmlDoc
 
You can access them like this:

Code:
set xmlDoc = CreateObject("MSXML2.DOMDocument")

xmlDoc.load(xmltext)

Response.write xmlDoc.getElementsByTagName("NodeID").item(index).text

 
That seems logical but I get this error

Microsoft VBScript runtime (0x800A01A8)
Object required: 'xmlDoc.getElementsByTagName(...).item(...)'
/callcenter/DisplayFileXml.asp, line 24

Im assuming by NodeID I have to put the element name

 
Here is an example of how it works

XML
Code:
<?xml version="1.0"?>
    <root>
         <child>
               <subchild>Text</subchild>
         </child>
         <child>
               <subchild>Text1</subchild>
         </child>
    </root>

ASP
Code:
<%
dim m_objXML

Set m_objXML = Server.CreateObject("MSXML2.DOMDocument")

m_objXML.load("XML")'Assuming the XML is a file
'm_objXML.loadxml ("XML") Assumes XML is a string

Response.write m_objXML.getElementsByTagName("subchild").item(0).text & "<BR>"

Response.write m_objXML.getElementsByTagName("subchild").item(1).text & "<BR>"

Set m_objXML = nothing
%>

This should return

Text
Text1

If you have multiple tags of the same name like above the index number starts at 0 and increases by 1 until all tags are available. Your first tag is always index 0. If it is the only tag then it will just be 0.

Let me know if you need more.

Cassidy

 
Seems I'm either shorting something to be installed on my server cause I coppied your example strate as is and still geting the same freakin error

Microsoft VBScript runtime (0x800A01A8)
Object required: 'getElementsByTagName(...).item(...)'
 
If your missing the XML objects you can download them here:


Otherwise that error would reflect the node name might be different. Remember node names are case sensitive.

See if either of those things help. Another thing you can try is to open up the registry and do a search for MSXML2.DOMDocument. If you find it in the registry then you have all the services installed it is just a problem with the nodename or index number.

Let me know

Cassidy
 
Hi got it going this way of some code I found somewhere
Thanks you help a lot cheers.
Code:
 Set xmlDoc = Server.CreateObject("msxml2.DOMDocument")
    ' Allow the document to complete loading
    xmlDoc.async = False
    success = xmlDoc.LoadXml(xmltext)
    If success = True Then
        Set root = xmlDoc.documentElement
        strData = root.childNodes.Item(0).childNodes.Item(0).Text
        Response.Write strName & ": " & strData
        Set root = Nothing
    End If
	
  Set xmlDoc = Nothing
 
How would I access the XML attributes?

for example...
Code:
  <?xml version="1.0" ?> 
- <!--  This file represents a fragment of a book store inventory database 
  --> 
- <bookstore>
- <book [b]genre="autobiography"[/b] publicationdate="1981" ISBN="1-861003-11-0">
  <title>The Autobiography of Benjamin Franklin</title> 
- <author>
  <first-name>Benjamin</first-name> 
  <last-name>Franklin</last-name> 
  </author>
  <price>8.99</price> 
  </book>
- <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
  <title>The Confidence Man</title> 
- <author>
  <first-name>Herman</first-name> 
  <last-name>Melville</last-name> 
  </author>
  <price>11.99</price> 
  </book>
- <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
  <title>The Gorgias</title> 
- <author>
  <name>Plato</name> 
  </author>
  <price>9.99</price> 
  </book>
  </bookstore>

All hail the INTERWEB!
 
Typically you would need to start another thread for a question this old but normally I would use this method to access the attrabutes:

Also assuming your using the above strategy to access the xml Document
Code:
for each xmlatt in xmldoc.attributes
     Response.write xmlatt.nodename & ": " & xmlatt.text & "<br>"
next

Let me know if you need a better example. I am in meetings today but bounce back and forth.

Cassidy
 
hmm..I seem to get an error running that, perhaps I've made an error elsewhere. Code follows...

Code:
	Response.Buffer = True
	Dim objXMLHTTP, xml
	' Create an xmlhttp object:
	Set xml = Server.CreateObject("Microsoft.XMLHTTP")
	' Opens the connection to the remote server.
	xml.Open "GET", "books.xml"
	'  Sends the request and returns the data:
	xml.Send
	xmltext = xml.responseText
	Set xml = Nothing


	Set xmlDoc = Server.CreateObject("msxml2.DOMDocument")
	' Allow the document to complete loading
	xmlDoc.async = False
	success = xmlDoc.LoadXml(xmltext)
	If success = True Then
		Set root = xmlDoc.documentElement
		'strData = root.childNodes.Item(0).childNodes.Item(0).Text
		'Response.Write(strData)
		Response.Write(xmlDoc.getElementsByTagName("book").item(0).text) & "<BR>"
		'Response.Write(xmlDoc.getElementsByTagName("book").item(0).attributes.)
		[b]For each xmlatt in xmlDoc.attributes[/b]
			response.write xmlatt.nodename '& ": " & xmlatt.text & "<BR>"
		Next
		Set root = Nothing
	Else
		Response.Write("Fail")
	End If
    
	Set xmlDoc = Nothing

my error...

Code:
Error Type:
Microsoft VBScript runtime (0x800A01C3)
Object not a collection
/NetTests/CiscoTests/asp/testread.asp, [b]line 31[/b]

Thanks for your help, Cassidy

All hail the INTERWEB!
 
change this

response.write xmlatt.nodename '& ": " & xmlatt.text & "<BR>"


to something like this

response.write xmlatt.nodename '& ": " & xmlDoc.getElementsByTagName("book").item(0).text& "<BR>"

-DNG
 
hey DNG,
thanks for your post....I'm gettin the error on the "For each xmlAtt in xmlDoc.attributes"

So I don't really get far enough to see if that would fix anything...

All hail the INTERWEB!
 
actually when you see that error its because you cannot refer to the text directly in your loop...i guess you need to use your node element...

-DNG
 
Couple of things: you can do this to get a single node and access its attribute values:

(Assuming oXML is set as the XML Document Root)
Code:
sPath = "//book[@ISBN='1-861003-11-0']"
set oMyBook = oXML.selectSingleNode(sPath)

response.write(oMyBook.getAttribute("genre"))

or to iterate through a collection of nodes:
Code:
sPath = "//book"
set oAllBooks = oXML.selectNodes(sPath)

for each oBook in oAllBooks
  response.write(oBook.text & ":" & oBook.getAttribute("ISBN"))
next

The variable sPath holds the XPath query, which is a way of selecting / filtering what you want out of an XML document. This is a very useful method when using XML. More Details here:
It's another way of getting information from the XML document, and can (in most circumstances) be easier and more efficient.

Please note that the above are abstracted examples, you will need to change the XPath / node details etc for your actual XML Document, and dim all the variables etc

Hope that helps.

btw: I use server.createobject("Msxml2.DOMDocument.3.0") for my XML document handling and server.createobject("server.XMLHTTP") for server side XMLHTTP. Just a thought.

A smile is worth a thousand kind words. So smile, it's easy! :)
 

oops, sorry, the last bit for XMLHTTP should read: server.createObject("MSXML2.ServerXMLHTTP.4.0")



A smile is worth a thousand kind words. So smile, it's easy! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top