Selecting certain XML elements

This is a discussion on "Selecting certain XML elements" within the Other Programming Languages section. This forum, and the thread "Selecting certain XML elements are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > Other Programming Languages

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Dec 2nd, 2005, 18:06
Junior Member
Join Date: Nov 2005
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Selecting certain XML elements

Hello All,
I have a question regarding searching and selecting certain elements in an XML document using asp. The xml script basically consist of news headings, contents, date, and category elements that tell what the news is related to and where it should be displayed and stored. For example Finance catagory stories will be stored in a different database to the business one.
Code: Select all
<Article Created="16:01:59" ID="15105602">
   <Heading>Equitable drops claim against former directors</Heading> 
   <Date>02/12/2005</Date> 
  <Contents>
   <news story goes in here> 
   </Contents>
  <Categories>
    <Category ID="430009725">Finance</Category> 
   <Category ID="430009734">Economy</Category> 
   <Category ID="430009735">Business</Category> 
    <Category ID="438000159">Insurance</Category> 
  </Categories>
</Article>
So basically I need a script that can check the XML for certain stories and store them in a database. I already have the script (see below) that takes ALL the news stories from the XML file and puts them in a database. So how can I get the script to check if a story is, say a Finance story and continue to store it in a database? Would I use an if else statment? if so, where should I put it?
Code: Select all
Dim XMLDom
Dim ItemID
Dim DbConn
Dim SQLString
Dim ANArticleNode
Dim CollectionOfArticleNodes
Dim cst
Set XMLDom = CreateObject("MSXML2.DomDocument.4.0")
XMLDom.async = False
XMLDom.setProperty "ServerHTTPRequest", True
Set DbConn = Createobject("adodb.connection")
'DbConn.open "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost; DATABASE=pokernewsxml; OPTION=3"
DbConn.open "Driver={MySQL ODBC 3.51 Driver};" & _ 
        "Server=82.195.128.88;" & _ 
        "Database=johnfog_xml;" & _ 
        "Uid=johnfog_xml;" & _ 
        "Pwd=wordword;"

'-- Load the XML data from your live URL
XMLDom.Load("http://feeds.directnews.org.uk/?ad96...2-ad546b2ed850") 
'-- Create a reference to a collection of all Article Tags within the downloaded XML Document
Set CollectionOfArticleNodes = XMLDom.SelectNodes("InfoStreamResults/Article")
'-- Iterate the collection of Article Tags 
For Each ANArticleNode in CollectionOfArticleNodes 
 ItemID = ANArticleNode.SelectSingleNode("@ID").text
 Heading = ANArticleNode.SelectSingleNode("Heading").text 
 Contents = ANArticleNode.SelectSingleNode("Contents").text
 sDate = ANArticleNode.SelectSingleNode("Date").text
 '-- Delete the item from the local database if it exists
 SQLString = "DELETE FROM DeHavillandNews WHERE trim(ItemID)='" & trim(ItemID) & "';"
    DbConn.Execute(SQLString)
 
 '-- Insert the item into the local database
 SQLString = "INSERT INTO DeHavillandNews (ItemID,Heading,Contents,strDate) " _
    & "VALUES('" & ItemID & "','" & EncodeIt(Heading) & "','" & EncodeIt(Contents) & "', '" & sDate & "');" 
 DbConn.Execute(SQLString) 
 
Next
 
'-- Handles quotations in text
Function EncodeIt(TextString)
 TextString = Replace(CStr(TextString), "''", "'")
 TextString = Replace(TextString, "'", "''")
 EncodeIt = TextString
End Function
Any help would be greatly appreciated as I am seriously stuck with this one.
Thanks folks
J
Reply With Quote

  #2 (permalink)  
Old Dec 5th, 2005, 10:31
Junior Member
Join Date: Nov 2005
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Selecting certain XML elements

problem solved
If anyone is interested, here is the solution:
Code: Select all
Dim XMLDom
Dim ItemID
Dim DbConn
Dim SQLString
Dim ANArticleNode
Dim ANArticleNode2
Dim CollectionOfArticleNodes
Dim CollectionOfArticleNodes2
Dim cst
Set XMLDom = CreateObject("MSXML2.DomDocument.4.0")
XMLDom.async = False
XMLDom.setProperty "ServerHTTPRequest", True
Set DbConn = Createobject("adodb.connection")
DbConn.open "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=test1.mdb"
'-- Load the XML data from your live URL
XMLDom.Load("http://feeds.directnews.org.uk/?ad96...2-ad546b2ed850") 
'-- Create a reference to a collection of all Article Tags within the downloaded XML Document
Set CollectionOfArticleNodes = XMLDom.SelectNodes("InfoStreamResults/Article")
'-- Iterate the collection of Article Tags 
For Each ANArticleNode in CollectionOfArticleNodes 
 '-- Now create a reference to the category tag
 Set CollectionOfArticleNodes2 = ANArticleNode.SelectNodes("Categories/Category")
 '-- And iterate through the nodes to test for a match
 For Each ANArticleNode2 in CollectionOfArticleNodes2
  ItemID = ANArticleNode2.SelectSingleNode("@ID").text
  if ItemID = "430009735" then
   '-- Retrieve the value of the heading node from the current article
   Heading = ANArticleNode.SelectSingleNode("Heading").text
 
   '-- Insert the item into the local database
   SQLString = "INSERT INTO test (Heading) " _
   & "VALUES('" & EncodeIt(Heading) & "');" 
   DbConn.Execute(SQLString) 
  end if
 Next '-- check the next category ID
Next '-- move to the next article
'-- Handles quotations in text
Function EncodeIt(TextString)
 TextString = Replace(CStr(TextString), "''", "'")
 TextString = Replace(TextString, "'", "''")
 EncodeIt = TextString
End Function
%>
Reply With Quote
  #3 (permalink)  
Old Dec 5th, 2005, 11:18
Tim356's Avatar
Reputable Member
Join Date: Nov 2003
Location: Australia
Age: 25
Posts: 331
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Tim356
Re: Selecting certain XML elements

Gotta love those questions you answer yourself! Good stuff.
Reply With Quote
  #4 (permalink)  
Old Dec 5th, 2005, 12:49
Junior Member
Join Date: Nov 2005
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Selecting certain XML elements

haha! true
Reply With Quote
Reply

Tags
selecting, certain, xml, elements

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
selecting PHP code - errors c010depunkk Webforumz Suggestions and Feedback 2 Jan 17th, 2008 11:57
no selecting data franknu PHP Forum 4 May 26th, 2007 18:08
Problem selecting elements Echilon Web Page Design 2 Jan 3rd, 2007 11:43
Selecting Numbers WillisTi Flash & Multimedia Forum 1 Nov 16th, 2005 16:56
selecting a value from array Ozeona Flash & Multimedia Forum 2 Jul 27th, 2005 10:14


All times are GMT. The time now is 01:25.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43