JavaScript Question: How do I Work with XML Data?
Вставка
- Опубліковано 7 вер 2024
- In this JavaScript tutorial we look at how to convert XML data to an XMLDom and then commands that can be used to access and manipulate that data.
Would you like to help keep this channel going?
/ allthingsjavascript
For a complete list of all our tutorials:
www.allthingsja...
Access to EVERY course (get 2 months free): www.skillshare...
Courses offered on Udemy at a discount (access from my site): allthingsjavasc...
#javascript #AllThingsJavaScriptLLC
UPDATE: When you receive the XML data, you may not receive it as a string. If that is the case you don't need to do the parseFromString method. You can begin to minipulate the XML as it is.
Oh that saved a lot of time, couldnt load the metadata from an Odata-Service in json, and i dont know how to work with xml, Thank you
Thanks for such a clear explanation of everything I needed to know for my particular xml situation, I was drowning in info I just couldn't grasp before I discovered this. I have subscribed!
Hang on... How do you get your separate xml file to be a string inside your html page??
That is how XML data will be passed to you. Are you receiving from a site?
@@AllThingsJavaScript I have a plain text xml file done in a word processor, like in your tutorial. I'd really prefer to leave it as a separate file that gets pulled into the html, presumably in the head section, using some sort of link or import statement. I've found out that this can be done with Javascript with a kind of statement. But can you also do this sort of thing with xml too (and if so what 'type' is it)? But I'm guessing that maybe the < and > all over the place may be interpreted as scripting errors, so maybe the < script... tag isn't the right tag to use.
@@andymurray4475 Here are some tutorials that my help: ua-cam.com/video/EF6Ua0cC4jY/v-deo.html and ua-cam.com/video/0gON4MUdJE8/v-deo.html
@@AllThingsJavaScript Those are just what I needed - and you've given me a choice of 2 ways. I think fetch looks easier. Thank you so much for your help.
Nice addition! I can see using this when working with GPS data in a .gpx or .kml format.
thank you
Hi I'm currently workng on a school project, and I'm struggling a bit with it. Has any of you created an XML file completly from scratch with JavaScript, I got a html form and I need to parse it to XML with an specific structure. I already saved all the data into a array, but I don't find any video or website with such an explanation.
I'm working in VSCode, when I try to use the DOMParse I get as a result "DOMParse is not defined". Why could that be?
Are you entering DOMParse or DOMParser? It should be DOMParser. Also, it should be used in a browser, so if you are using Node that could be an issue.
@@AllThingsJavaScript Yes, I'm not using it in a browser. Ok, I didn't know that. Thanks for your reply. Cheers
We've come a long way from getting XML responses from web services.
Hello Sir, I have a sample scenario like element. Attribute and in that there are same element more than one 1 attribute, Example text = "king" , text1 ="queen", text2 ="prince" ,...Here i need to get the first value from result element
I would probably retrieve the entire text from the result element and then use a different technique. For example convert it to an array on commas and then extract.
@@AllThingsJavaScript Thank you Sir, i will try it out
But what would be the best way to get and handle the xml data, if you are dealing with multiple xml files and some data is being repeated and updated there?
If XML data changes you need to reload. If data is changing frequently you probably want to use a different way of storing the data rather than XML. If you need to load multiple XML files, you use the same technique to load and work with that. Maybe you load all the XML data you need and then parse it into a single object to make it easier to work with.
thanks for the awesome tutorial. I have an additional question. I have to parse XML-Data which is also structured with different ns-prefixes... how can I acces those namespaces with the DOM-Parser?
Can you provide an example of the XML?
thanks awsome.I have been searching for a way to validate the syntax of an xml file using domparser. sometimes a tag bracket is missing from one or more elements, though you still can parse it with domparser, can't you?
Not sure but should be easy to figure out. Give it a try and let us know what you discover.
Can you show an example of modifying an attribute value and writing it back to the original XML file please? I am able to modify the data but stuck in writing it back to the original file.
If you're doing this in a browser, there are certain restrictions with writing files. You could do this with Node.
@@AllThingsJavaScript I am not doing this on a browser...I am doing it it on an XML file stored locally
Code Sample:
var fs = require('fs');
var libxml = require("libxmljs");
const { DOMParser } = require('xmldom');
const parser = new DOMParser();
const { XMLSerializer } = require('xmldom');
const serializer = new XMLSerializer();
xml2js = require("xml2js");
var builder = new xml2js.Builder();
fs.readFile('/.afile','utf8',function(err,mainfileh) {
if (err) {
console.log(err);
}
var dom = parser.parseFromString(mainfileh,"text/xml");
conditions = xmlmainDoch.getElementsByTagName('condition').length;
for (var i = 0; i
@@AllThingsJavaScript I am not doing this on a browser...I am doing it it on an XML file stored locally
Code Sample:
var fs = require('fs');
var libxml = require("libxmljs");
const { DOMParser } = require('xmldom');
const parser = new DOMParser();
const { XMLSerializer } = require('xmldom');
const serializer = new XMLSerializer();
xml2js = require("xml2js");
var builder = new xml2js.Builder();
fs.readFile('/.afile','utf8',function(err,mainfileh) {
if (err) {
console.log(err);
}
var dom = parser.parseFromString(mainfileh,"text/xml");
conditions = xmlmainDoch.getElementsByTagName('condition').length;
for (var i = 0; i
How do i add an event listener to a xml tag?
Event listeners are for the DOM not XML. Sorry
Is it possible to pass the xml object in window.open('url)..?
how to open or render the xml data in new tab ..?
So you want to display information from the XML file in a new tab? You would need to communicate between windows. Maybe this tutorial would help: ua-cam.com/video/GNZg1KRsWuU/v-deo.html
Do I need to import something to use DOMParser? It's not showing up for me
The DOMParser is a part of the Web API for your browser. You can check to see if your browser supports it here: developer.mozilla.org/en-US/docs/Web/API/DOMParser
@@AllThingsJavaScript thanks, I'm not running my script through a browser but I found a way to do it using the XML class in React, I think
@@RobertShane That would explain it. DOMparser is a part of the browser. Glad you figured something out.
Nice video Thanks!!
Thanks buddy.
Can this work the same with a svg file?
svg is xml-based, but its purpose would be different. Yes, some of the things in this tutorial could be used to work with svg depending on what you were trying to do.
What if the xml and javascript is in two separate files in the same folder?
You need to load the XML file. I will plan on doing a tutorial on that.
Just released a tutorial on this topic: ua-cam.com/video/EF6Ua0cC4jY/v-deo.html
awesome I was wondering the same thing, checking it out now.
Quit after he didnt show how we actually get the string from the xml file. Dont know whats up with those tutorials. They all start in the middle of the process