Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Mike Mitchell
Courses Plus Student 27,026 PointsHow to work with an XML file
Hello out there! I need to find a language that can help me solve a problem. I've dabbled with Python and JavaScript but think maybe I'm on the wrong track.
Here's my problem. I have to manually download a large XML file from our database of users and search through it using a text editor to find specific types of users, copy their usernames, and add them to another system by copying and pasting.
It's incredibly tedious and I'm hoping to figure out a way to programmatically do it.
I see a lot of activity on the forums with XML and Android but I don't think this is the way to go.
Do any of you have any suggestions on which path I should look into when working with XML? Should I find a way to change it to JSON?
Thanks for any advice you have!
2 Answers

Matt F.
9,518 PointsHi Mike,
It would almost certainly be easier to programmatically filter the data in JSON format. There are a variety of converters out there that you could use, including nom packages (commonly referred to as node package manager).
Once you get it into JSON format, you could do something like this:
(Dependent on you having node installed and importing node fs as fs)
Read the JSON file with fs.readFileSync() and store it in a variable - let's say it is called originalData. For the purpose of this example, I will just set it to an array that is representative of what you would receive from reading the given file.
PS: If you know how to handle asynchrony well, you can use fs.readfile() instead of fs.readFileSync(), but it should not be that big of a deal for this task either way.
var originalData = [
{
customerName: 'Bob',
type: 'normal'
},
{
customerName: 'Sue',
type: 'super'
},
{
customerName: 'Ralph',
type: 'super'
},
{
customerName: 'Sue',
type: 'super'
}
];
Your list of data is certainly more complex than this, but the basic idea of filtering an array of objects (should convert the JSON to an actionable array automatically, but you can always use originalData = JSON.parse(originalData) to be safe.) is the same. In this case, we will filter (a method on the Array prototype) out only the customers that are the type 'super' and store it in a variable called filteredData
var isSuper = function(customer) {
return customer.type === 'super';
};
var filteredData = originalData.filter(isSuper)
Once you run this code, the filteredData will contain an array of the Objects Sue and Ralph. Since Bob and Brock were not of the 'super' type, they were not added the the new array that we build.
The callback function (in our case isSuper) is key to what items are added to the new array. Items that evaluate to true will be added, while those that evaluate to false will not. For example:
var allAdded = function(customer) {
return true;
};
var filteredData = originalData.filter(allAdded)
will result in all customers being added to the filteredData array - while
var noneAdded = function(customer) {
return false;
};
var filteredData = originalData.filter(noneAdded)
will result in an empty array.
Once you complete the filtration that you want, then you would use fs.writefile(path, filteredData) to write the filteredData out to a new file.

Mike Mitchell
Courses Plus Student 27,026 PointsMatt,
This was so much easier to implement than I thought. I jumped on the npm site, found xml2js and started working with the data in under an hour.
Many, many thanks!
Mike
Mike Mitchell
Courses Plus Student 27,026 PointsMike Mitchell
Courses Plus Student 27,026 PointsMatt,
This is awesome and it makes a lot of sense to me. I've worked with Node so I'm happy to see you mentioned it though I haven't thought to explore XML converters (I'm not that well-versed in Node).
I'm going to try this out. I really appreciate the help and the examples you've given! Seriously so cool.
Mike
Matt F.
9,518 PointsMatt F.
9,518 PointsGlad that I could help =)