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

КОМЕНТАРІ • 51

  • @AllThingsJavaScript
    @AllThingsJavaScript  5 років тому +3

    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.

  • @raphaelsack6983
    @raphaelsack6983 5 років тому +1

    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

  • @andymurray4475
    @andymurray4475 4 роки тому

    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!

    • @andymurray4475
      @andymurray4475 4 роки тому

      Hang on... How do you get your separate xml file to be a string inside your html page??

    • @AllThingsJavaScript
      @AllThingsJavaScript  4 роки тому

      That is how XML data will be passed to you. Are you receiving from a site?

    • @andymurray4475
      @andymurray4475 4 роки тому

      @@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.

    • @AllThingsJavaScript
      @AllThingsJavaScript  4 роки тому

      @@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

    • @andymurray4475
      @andymurray4475 4 роки тому

      @@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.

  • @rivertaig8703
    @rivertaig8703 5 років тому

    Nice addition! I can see using this when working with GPS data in a .gpx or .kml format.

  • @videotimebaby90
    @videotimebaby90 2 роки тому

    thank you

  • @canete_code
    @canete_code 4 роки тому

    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.

  • @Doctor_Frog_9000
    @Doctor_Frog_9000 2 роки тому

    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?

    • @AllThingsJavaScript
      @AllThingsJavaScript  2 роки тому

      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.

    • @Doctor_Frog_9000
      @Doctor_Frog_9000 2 роки тому

      @@AllThingsJavaScript Yes, I'm not using it in a browser. Ok, I didn't know that. Thanks for your reply. Cheers

  • @joemtz8478
    @joemtz8478 3 роки тому +1

    We've come a long way from getting XML responses from web services.

  • @Vasanth251180
    @Vasanth251180 3 роки тому

    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

    • @AllThingsJavaScript
      @AllThingsJavaScript  3 роки тому

      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.

    • @Vasanth251180
      @Vasanth251180 3 роки тому

      @@AllThingsJavaScript Thank you Sir, i will try it out

  • @user-zr7ve3ut5t
    @user-zr7ve3ut5t 3 роки тому

    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?

    • @AllThingsJavaScript
      @AllThingsJavaScript  3 роки тому

      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.

  • @benjaminauer788
    @benjaminauer788 3 роки тому

    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?

  • @anaskala1196
    @anaskala1196 3 роки тому

    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?

    • @AllThingsJavaScript
      @AllThingsJavaScript  3 роки тому

      Not sure but should be easy to figure out. Give it a try and let us know what you discover.

  • @sarveshsridhar4465
    @sarveshsridhar4465 3 роки тому

    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.

    • @AllThingsJavaScript
      @AllThingsJavaScript  3 роки тому

      If you're doing this in a browser, there are certain restrictions with writing files. You could do this with Node.

    • @sarveshsridhar4465
      @sarveshsridhar4465 3 роки тому

      @@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

    • @sarveshsridhar4465
      @sarveshsridhar4465 3 роки тому

      @@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

  • @shifter11101
    @shifter11101 3 роки тому

    How do i add an event listener to a xml tag?

  • @vijaykumarkalapala1648
    @vijaykumarkalapala1648 5 років тому

    Is it possible to pass the xml object in window.open('url)..?
    how to open or render the xml data in new tab ..?

    • @AllThingsJavaScript
      @AllThingsJavaScript  5 років тому

      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

  • @RobertShane
    @RobertShane 4 роки тому

    Do I need to import something to use DOMParser? It's not showing up for me

    • @AllThingsJavaScript
      @AllThingsJavaScript  4 роки тому

      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

    • @RobertShane
      @RobertShane 4 роки тому

      @@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

    • @AllThingsJavaScript
      @AllThingsJavaScript  4 роки тому

      @@RobertShane That would explain it. DOMparser is a part of the browser. Glad you figured something out.

  • @meenalnafde6216
    @meenalnafde6216 5 років тому

    Nice video Thanks!!

  • @slapmyfunkybass
    @slapmyfunkybass 6 років тому

    Thanks buddy.

  • @NekoMoo
    @NekoMoo 5 років тому

    Can this work the same with a svg file?

    • @AllThingsJavaScript
      @AllThingsJavaScript  5 років тому

      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.

  • @snotool
    @snotool 6 років тому

    What if the xml and javascript is in two separate files in the same folder?

    • @AllThingsJavaScript
      @AllThingsJavaScript  6 років тому

      You need to load the XML file. I will plan on doing a tutorial on that.

    • @AllThingsJavaScript
      @AllThingsJavaScript  6 років тому +1

      Just released a tutorial on this topic: ua-cam.com/video/EF6Ua0cC4jY/v-deo.html

    • @BruceChastain
      @BruceChastain 5 років тому

      awesome I was wondering the same thing, checking it out now.

  • @Laurenan69
    @Laurenan69 5 років тому +5

    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