简体   繁体   中英

How do I take returned XML and insert specific elements into columns in SQL Server using VB?

I have a SQL Server database, and I need to populate it with returned xml from an api call. This is the xml code that's returned(not in a file):

<petfinder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xsi:noNamespaceSchemaLocation="http://api.petfinder.com/schemas/0.9/petfinder.xsd">
<header>
 <version>0.1</version>
 <timestamp>2013-04-08T14:52:23Z</timestamp>
 <status>
  <code>100</code>
  <message/>
 </status>
</header>
<lastOffset>25</lastOffset>
 <pets>
  <pet>
   <id>18589607</id>
   <shelterId>OK98</shelterId>
   <shelterPetId>11C-0015</shelterPetId>
   <name>Sam</name>
   <animal>Cat</animal>
   <breeds>
    <breed>Domestic Short Hair</breed>
    <breed>Tabby</breed>
   </breeds>
   <mix>yes</mix>
   <age>Adult</age>
   <sex>M</sex>
   <size>XL</size>
   <options>
    <option>altered</option>
    <option>hasShots</option>
    <option>housebroken</option>
   </options>
  <description>
   <![CDATA[
    <div>This guy loves the camera. Look at him pose and show off! Sam is about 5 years old and is a cream Tabby. He is good with other cats and is house trained. He has turquoise eyes and is a sweet sweet cat. Sam loves to be the right hand man and assist you on any task you may have. Sammy is not the type of cat that likes to be held but will sit right next to you for some rubbing and head butting. Our adoption fee is $100 for dogs and $75 for cats. This adoption fee includes the spay or neutering and rabies shot. </div>
   ]]>
  </description>
  <lastUpdate>2012-07-24T14:50:17Z</lastUpdate>
  <status>A</status>
  <media>
   <photos>
    <photo id="1" size="x">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-1-x.jpg
    </photo>
    <photo id="1" size="fpm">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-1-fpm.jpg
    </photo>
    <photo id="1" size="pn">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-1-pn.jpg
    </photo>
    <photo id="1" size="pnt">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-1-pnt.jpg
    </photo>
    <photo id="1" size="t">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-1-t.jpg
    </photo>
    <photo id="2" size="x">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-2-x.jpg
    </photo>
    <photo id="2" size="fpm">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-2-fpm.jpg
    </photo>
    <photo id="2" size="pn">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-2-pn.jpg
    </photo>
    <photo id="2" size="pnt">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-2-pnt.jpg
    </photo>
    <photo id="2" size="t">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-2-t.jpg
    </photo>
    <photo id="3" size="x">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-3-x.jpg
    </photo>
    <photo id="3" size="fpm">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-3-fpm.jpg
    </photo>
    <photo id="3" size="pn">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-3-pn.jpg
    </photo>
    <photo id="3" size="pnt">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-3-pnt.jpg
    </photo>
    <photo id="3" size="t">
    http://photos.petfinder.com/photos/US/OK/OK98/18589607/OK98.18589607-3-t.jpg
    </photo>
   </photos>
  </media>
  <contact>
   <address1>714 Martin Luther King Jr Ave</address1>
   <address2/>
   <city>Duncan</city>
   <state>OK</state>
   <zip>73533</zip>
   <phone/>
   <fax/>
   <email/>
  </contact>
 </pet>
...

More specifically, I need to take the nodes for ID, name, animal, description, and several others, and insert them into their respectful columns in my database.

And it must repeat this for each "pet" node that these are all in.

Can I do this in VB.net without saving a file, just as an xml string?

Please help, I've been stuck on this for days.

Assuming you have your XML structure in a variable (or stored procedure parameter) of type XML , you can do something like this:

CREATE PROCEDURE dbo.InsertXmlData 
     @XmlData XML
AS BEGIN
   INSERT INTO dbo.YourTable(ID, PetName, Animal, Description)
      SELECT
         ID = Pet.value('(id)[1]', 'int'),
         PetName = Pet.value('(name)[1]', 'varchar(50)'),
         Animal = Pet.value('(animal)[1]', 'varchar(50)'),
         [Description] = Pet.value('(description)[1]', 'varchar(500)')
      FROM 
         @XmlData.nodes('/petfinder/pets/pet') AS xTBL(Pet)
END

That gives you the info in those nodes as a set of rows and columns which you can easily insert into a SQL Server table. So now you just need to find a way to call this stored procedure from your VB.NET code and pass in the XML into the @XmlData parameter

Here's an example of how you can extract the data for each pet from the XML using XPath and the XmlDocument class:

Dim doc As XmlDocument = New XmlDocument()
doc.LoadXml(xmlString)
For Each pet As XmlNode In doc.SelectNodes("/petfinder/pets/pet")
    Dim id As String = pet.SelectSingleNode("id").InnerText
    Dim name As String = pet.SelectSingleNode("name").InnerText
    ' ...
Next

I'm assuming you know how to save the data to your SQL database from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM