Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - Help: reading XML elements with asp >Thread Next - Re: reading XML elements with asp Re: reading XML elements with aspTo: NULL Date: 7/2/2004 2:26:00 PM "sismis" <zbosnjak@g...> wrote in message
news:cc3n4e$q1m$1@l......
> Hi!
>
> I need to fill the database with selected elements from XML, but cant get
> them apart.
>
> items.xml file:
> <?xml version="1.0" encoding="UTF-8"?>
> <dataroot xmlns:od="urn:schemas-microsoft-com:officedata"
> xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
> xsi:noNamespaceSchemaLocation="items.xsd">
> <item>
> <class>2</class>
> <type>0</type>
> <code>0101002</code>
> </item>
> <item>
> <class>3</class>
> <type>0</type>
> <code>0102008</code>
> </item>
> <item>
> <class>4</class>
> <type>0</type>
> <code>0101018</code>
> </item>
> ...
> </dataroot>
>
>
>
>
> ASP file:
> <%
> Dim sourceFile, source, rootElement, HTMLCode
> 'define xml file
> sourceFile = Server.MapPath("items.xml")
> ' Create an instance of the XML parser and load the XML into the DOM
> Set source = Server.CreateObject("Microsoft.XMLDOM")
> source.async = false
> source.load sourceFile
> Set rootElement = source.documentElement
> ' list them out
> For i = 0 To (rootElement.childNodes.length -1)
> Response.Write(rootElement.childNodes(i).text & "<br>")
> ' the misty part here
> Next
>
> The question: how to get elements by name into variable, say just <type>
> and
> <code>, getElementsByTagName("ntype").item(0).text or???
>
>
>
> Thnx
>
>
Firstly do not use "microsoft.xmldom". Find out what version your server
has, hopefully version 4.0, and use the specific id, e.g.
"MsXml2.DomDocument.4.0".
Next you're better placed if you use XPath to select the nodes you want
rather than use the DOM:
Set source = Server.CreateObject("MsXml2.DomDocument.4.0")
source.async = false
source.load sourceFile
Set rootElement = source.documentElement
Set colNodes = rootElement.selectNodes("item")
For i = 0 To colNodes.length - 1
Set nodeType = colNodes(i).selectSingleNode("type")
Set nodeCode = colNodes(i).selectSingleNode("code")
Response.Write i & ") Type: " & nodeType.text & ", Code: " & nodeCode.text
& "<br>"
Next
--
Joe (MVP - xml)
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
