Parsing XML - Go Lang Practical Programming Tutorial p.11

sentdex · Beginner ·🛠️ AI Tools & Apps ·8y ago

Key Takeaways

This video tutorial series covers parsing XML documents using the Go programming language, specifically utilizing the encoding/xml package to unmarshal XML structures into Go structs. The tutorial demonstrates how to define and use structs, slices, and arrays to store and manipulate XML data, as well as how to use value receivers and string methods to format and display the data.

Full Transcript

what's going on everybody welcome to part 11 of the golang tutorial series in this part what we're gonna be doing is learning how we can actually parse this XML document so just in case for the inevitability when the washington post the sitemap index basically here when it ever it happens to change or whatever something else goes wrong because it almost certainly will especially on a long enough time line this is the structure of it and then at the end I'll kind of show you guys how you can convert that but just know that that's the structure and you can either use your own sitemap index that you found from sitemap rather that you find from somewhere else or or you can you can convert this basically so if you want to if you want to be able to do that I'll put a link or something to the text-based version in the tutorial so you can still follow along even if for whatever reason you can't use the exact same one that we're using but yeah so let's go ahead and get started so the way that we're going to do this is we're going to use one more package and that's the encoding slash HTML or XML package so that's going to be encoding slash XML and we're gonna use that to unmarshal into basically the structure that is at the XML structure so it's gonna we're gonna we could do this ourselves totally from scratch without using encoding ten XML or slash XML but that would be really tedious whereas this is kind of already built to accept it we just need to kind of give it the structure of the data that we're trying to decode really so let's go ahead and get started so the first thing that I'm gonna do is kind of clean this up we're not anymore gonna use string body or print that line out so now what we want to do is we need to define the structure of this this XML document so first I'm going to do a type sitemap index that's gonna be a struct and then inside it basically at the end of the day what do we want so we want capital locations as the value and it's going to be an array of the location type which doesn't yet exist and then we kind of scribe this is for when we go to unmarshal it the tag that it's under so that's XML : and then double quotes don't forget those sitemap the other thing you don't want to forget that's a little less obvious in my opinion is that you must capitalize these values if you don't capitalize these values they won't be exported when you go to use two unmarshal it basically it's gonna see that that's really supposed to be like internal basically so it won't export it you won't get any values from it and that's really annoying I got stuck on that for way too long that was annoying so anyways locations of location type and then what's happening here basically it's going to be and in this case a slice in slices basically let me just run through slices really quickly and erase basically anything that is you know square brackets with the number in it and then a type whatever that type happens to be that is that's an array anything that doesn't have a number in it and a type that's a slice they're pretty much the same thing the only difference is this is of a fixed size you could also have like a 5x5 for example that's going to be an array this is gonna be a slice so for example 5x5 int that's a you know 5x5 integer array whereas here this is just some sort of integer slice of some kind in our case here we've got locations it's a slice of location types we don't really know what those are yet and so we need to define those so what while we're talking about it let's go ahead and do that type location struct and here it's gonna be the location again don't forget it must be capital l OC string it's gonna so that's a string type and then where's it located that's gonna be XML under the Luke LOC tag obviously that must be lowercase because that's the you know the tag itself is lower cased okay now what we can do is come down here and and we can do bar s and bar s is going to be a sitemap index type and now we can unmarshal into that so we're gonna do XML dot capital u I'm Marshall and then where do we want to or what do we want to unmarshal that's gonna be bytes and then where do we want to well we're gonna unmarshal at the basically into the memory address of s so now that we've done that let's go ahead and see what we're looking at so we should be able to format dot print line s dot locations because that's gonna be our basically our our slice of data so let's go ahead and save that and run it and see how we've done go wrong go to okay so what we get here is pretty much like we expected and if you're not you know if you're not new to programming your pry some flags are going off but but anyways here are all the URLs so we're very very close to what we wanted but it looks odd like we can see the brackets here which kind of denotes list or array or something which is like yeah that's what we wanted but then we have like these curly braces well the reason why we have these curly braces is what we have here is it's still basically it's not a string yet like so so the sitemap index so like of this type yes it's got a location slice and yeah the location itself is a string but we actually need to have a string method that's gonna apply to this so we've actually already talked about methods and all that so this is relatively simple but in this case if you have a string method what are we trying to do are we trying to actually modify anything within the the struct or are we just trying to get some values out of it well we're just trying to get some values out of it so in this case we can use a value receiver so let's go ahead and func and then we're gonna do L for a location type that was an underscore L that looks kind of weird and sublime but anyways and then it's a string with a capital S of string type which is what it's going to return and then it's just going to return a format dot s print F L dot location and save that and then mistaken let's just rerun it real quick right okay so now that we've given it a string method it actually has strings lo and behold we actually have string URLs also let me just pull up the s printf here there you go anyway it basically it's just gonna format that it does what it says formats according to a format specifier and returns the resulting string basically you're gonna use that pretty much every time you will have a string method if you want to convert some sort of struct thing to a string this would be the way you're going to use it to be honest I've not really seen any other reason you would use s Peart if that's the only time I'm sure there are more I've not been in golang for a really long time but that's appears to me to be the the main use okay so now that we've made it that far we've got a it's a slice but I'm gonna probably call it a list a few times but and that's what it looks like to me it's a list of stuff right but it's definitely a slice there's no comma there so I guess we could call it not a list anyway what we need to do now is iterate through these values and get those URLs and then visit those URLs and because those are site maps get the URLs and maybe titles or something from those site maps and so on so that's what we're doing the next tutorial obviously we need to learn how to actually loop over this list first that's what we're gonna be talking about loops next the other thing I want to show you guys real quick for the end of the tutorial if you if for whatever reason you can't access the Washington Post site map and you still kind of wanted to follow along here is what you could do you could save our wash post XML equals slice of bytes bite and then it's going to be a multi-line and paste boom done let's go ahead and move that underneath the import just to make it right and then basically a byte so you could say bytes each equals wash post I don't think it's what we want to like wash post XML yeah get rid of this unmarshal bytes we've probably got some import that we don't need but let's just run it really quickly to find bytes and I you util bytes equals that's kind of Oh : equals and then it was IO util that we didn't need so I can just remove that real quick bring this back up run again oh come on just please work this time I have time for this I just wanted to show you guys real quickly okay and then that's probably gonna get angry at us for using HTTP there we go okay so just obviously it's short so I use the shorter XML but that's how you can just still follow along if and when this the sitemap goes away also it'd be kind of nice because you can come in here and you can maybe add to your new tags kind of play around with it you know I encourage you to try your own sitemap index try to figure out how to build the struck sand all that because that's not the most intuitive thing ever in my opinion but anyway that's all for now in the next tutorial we'll talk about looping because we want to be able to loop over that list if you have questions comments concerns whatever feel free to leave them below otherwise I will see you in the next go tutorial

Original Description

Welcome to part 11 of the Go programming tutorial series. In this tutorial, we're going to cover how we can go about parsing this XML document. Text tutorials and sample code: https://pythonprogramming.net/go/ https://twitter.com/sentdex https://www.facebook.com/pythonprogramming.net/ https://plus.google.com/+sentdex
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from sentdex · sentdex · 0 of 60

← Previous Next →
1 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

This tutorial series teaches how to parse XML documents using Go, covering topics such as defining structs, using slices and arrays, and formatting strings. It provides a practical introduction to XML parsing and Go programming.

Key Takeaways
  1. Define a sitemap index struct with locations array
  2. Define a location struct with LOC string and URL string
  3. Unmarshal XML structure into sitemap index struct
  4. Add a string method to a struct using a value receiver
  5. Format a string using fmt.Sprintf
  6. Store a list of data in a slice
  7. Use HTTP to access the sitemap
  8. Try to build a sitemap index
💡 The encoding/xml package in Go provides a convenient way to unmarshal XML structures into Go structs, making it easier to work with XML data in Go programs.

Related AI Lessons

How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Learn to create a second version of yourself inside Obsidian using AI with a step-by-step guide
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Learn how to prepare for the Spain civil service TIC exam using AI in 2026, boosting your chances of success with technology-driven study techniques
Dev.to · David García
Going Viral! How I Created AI Kissing Videos Step by Step Easily Using AIAI.com
Create viral AI kissing videos using AIAI.com in a step-by-step process, leveraging AI technology for creative content creation
Medium · AI
How to prepare TIC teacher exams in Spain with AI (oposiciones 2026)
Prepare for TIC teacher exams in Spain using AI with these actionable steps
Dev.to AI
Up next
Low-Tech, High-Impact: Replacing Your Receptionist With a $15 AI Phone System
Maximum Lawyer
Watch →