XML Interaction
-
Hello, I am trying to make a small app to interact with Tumblr.com. The API information page is here: http://www.tumblr.com/docs/api It outputs XML which i am not familiar with....yet. What i want to know is how would i tell a vb app to download this output and what sort of variable should i keep it in to work with? Is string appropriate? The ANZAC
-
Hello, I am trying to make a small app to interact with Tumblr.com. The API information page is here: http://www.tumblr.com/docs/api It outputs XML which i am not familiar with....yet. What i want to know is how would i tell a vb app to download this output and what sort of variable should i keep it in to work with? Is string appropriate? The ANZAC
Imports System
Imports System.IO
Imports System.Net
Imports System.XmlModule Module1
Sub Main()
'Address of URL, just a random user
Dim URL As String = "http://harumacchi.tumblr.com/api/read"
' Get data
Dim client As WebClient = New WebClient()' Read an print to console Dim reader As StreamReader = New StreamReader(client.OpenRead(URL)) Dim str As String = "" Do While reader.EndOfStream = False str = reader.ReadLine() Console.WriteLine(str) Console.ReadKey() Loop ' load as document Dim xmldoc As XmlDocument = New XmlDocument() xmldoc.Load(client.OpenRead(URL)) For Each node As XmlNode In xmldoc.ChildNodes Console.WriteLine(node.Name) Console.ReadKey() Next ' read as xml Dim xmlreader As XmlTextReader = New XmlTextReader(client.OpenRead(URL)) Do While xmlreader.Read() If xmlreader.NodeType = XmlNodeType.Element Then Console.WriteLine(xmlreader.LocalName) Console.ReadKey() End If Loop End Sub
End Module
I personally prefer XmlDocument but you could also use XmlTextReader.
-
Imports System
Imports System.IO
Imports System.Net
Imports System.XmlModule Module1
Sub Main()
'Address of URL, just a random user
Dim URL As String = "http://harumacchi.tumblr.com/api/read"
' Get data
Dim client As WebClient = New WebClient()' Read an print to console Dim reader As StreamReader = New StreamReader(client.OpenRead(URL)) Dim str As String = "" Do While reader.EndOfStream = False str = reader.ReadLine() Console.WriteLine(str) Console.ReadKey() Loop ' load as document Dim xmldoc As XmlDocument = New XmlDocument() xmldoc.Load(client.OpenRead(URL)) For Each node As XmlNode In xmldoc.ChildNodes Console.WriteLine(node.Name) Console.ReadKey() Next ' read as xml Dim xmlreader As XmlTextReader = New XmlTextReader(client.OpenRead(URL)) Do While xmlreader.Read() If xmlreader.NodeType = XmlNodeType.Element Then Console.WriteLine(xmlreader.LocalName) Console.ReadKey() End If Loop End Sub
End Module
I personally prefer XmlDocument but you could also use XmlTextReader.
Thanks. I'm not sure exactly how to work with this though. If you are willing to help more here I have further questions. Alternatively I can make a new thread. For instance, how would i then get certain bits of information about each post? I gather the For Each loop can be used to probe the nodes, but how would i find the id or link for each post using this loop?
Please check out my articles: The ANZAC's articles
-
Thanks. I'm not sure exactly how to work with this though. If you are willing to help more here I have further questions. Alternatively I can make a new thread. For instance, how would i then get certain bits of information about each post? I gather the For Each loop can be used to probe the nodes, but how would i find the id or link for each post using this loop?
Please check out my articles: The ANZAC's articles
-
I'm a bit lost now. Don't you understand the tumbler/api xml or are you confused with the XmlDocument class?
I don't really understand how to use the xml document, how to look through the nodes etc and extract the information i want.
Please check out my articles: The ANZAC's articles
-
I don't really understand how to use the xml document, how to look through the nodes etc and extract the information i want.
Please check out my articles: The ANZAC's articles
XML Specification In short, XML is a structured document, conforming to a schema. Unfortunatly tumblr does not provide this schema (an XSD). XmlDocument class When you load an XML document into an XmlDocument instance it creates an XmlNode for every tag encountered and adds it as a childnode to its parent. To traverse all nodes you start at the XmlDocument instance (it actually is an XmlNode itself!). The HasChildren property indicates whether the node has children, you can iterate over the ChildNodes collection to get the nodes (which can also have children themselve). A node also has an Attributes collection which contains things like name, ID, type etc. that you see in the tags of the XML file To find specific node(s) there are methods like SelectNodes and SelectSingleNode. These methods use XPath syntax. It's possible to generate a classmodel for use in your program. You need to locate XSD.exe, it's somewhere in "C:\Program Files\Microsoft SDKs\Windows\...\bin" This utily can generate an XSD schema from an XML file and a .vb classfile from the XSD. So capture the output from http://(USER).tumblr.com/api/read, save as xxxx.xml and feed the file to XSD.exe. Then feed xxxx.xsd to XSD.exe in add the resulting xxxx.vb to your project. Make sure that the captured XML contains all tags and attributes described on the tumblr/api page