The getElementsByTagName function is failing to retrieve all of the child nodes

Looking for assistance with XML parsing

str = "<ROOT><CATEGORY_AREA_LIST><CATEGORY_AREA NAME='General'><CATEGORY_TYPE NAME='MOC'><PROPOSED_LEVEL NAME='3'></PROPOSED_LEVEL></CATEGORY_TYPE><CATEGORY_TYPE NAME='General'><PROPOSED_LEVEL NAME='3'></PROPOSED_LEVEL></CATEGORY_TYPE></CATEGORY_AREA><CATEGORY_AREA NAME='BOO'><CATEGORY_TYPE NAME='Open'><PROPOSED_LEVEL NAME='1'></PROPOSED_LEVEL></CATEGORY_TYPE><CATEGORY_TYPE NAME='NOC'><PROPOSED_LEVEL NAME='5'></PROPOSED_LEVEL></CATEGORY_TYPE></CATEGORY_AREA></CATEGORY_AREA_LIST></ROOT>"

I have a JavaScript function that's only returning one entry for CATEGORY_AREA, but I need to access all entries. Can you help me troubleshoot what might be going wrong?

My goal is to navigate through all category_area, category_type, and proposed_level elements in the XML string.

Your guidance is appreciated!

Answer №1

To view the XML document generated by the DOM Parser, simply insert console.log(xmlDoc) into your code.

https://i.sstatic.net/VDwkV.png

If there are errors present, you may notice that the CATEGORY_AREA_LIST element does not contain any child nodes.

You can utilize a tool like xmllint (readily available on most UNIX systems and even on Windows 10) to pinpoint specific errors within your XML structure.


Updated version:

After rectifying your XML file, the issue cannot be replicated anymore:

str = "<ROOT><CATEGORY_AREA_LIST><CATEGORY_AREA NAME='General'><CATEGORY_TYPE NAME='MOC'><PROPOSED_LEVEL NAME='3'></PROPOSED_LEVEL></CATEGORY_TYPE><CATEGORY_TYPE NAME='General'><PROPOSED_LEVEL NAME='3'></PROPOSED_LEVEL></CATEGORY_TYPE></CATEGORY_AREA><CATEGORY_AREA NAME='BOO'><CATEGORY_TYPE NAME='Open'><PROPOSED_LEVEL NAME='1'></PROPOSED_LEVEL></CATEGORY_TYPE><CATEGORY_TYPE NAME='NOC'><PROPOSED_LEVEL NAME='5'></PROPOSED_LEVEL></CATEGORY_TYPE></CATEGORY_AREA></CATEGORY_AREA_LIST></ROOT>"
var dp = new DOMParser();    
var xmlDoc = dp.parseFromString(str , "text/xml");
var list = xmlDoc.getElementsByTagName("CATEGORY_AREA_LIST");
var categoryArea = list[0].childNodes;
console.log(categoryArea.length)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

children blocking clicks on their parents' divs

I previously asked a question about a parent div not responding to click events because its children were blocking it. Unfortunately, I couldn't recreate the issue without sharing a lot of code, which I didn't want to do. However, since I am stil ...

Ensure that a select input, dynamically generated, is marked as 'required' only when other inputs have been filled out

Displayed below is a snapshot of a page I'm developing that allows users to input details about various rooms in a property. Users have the ability to 'add' multiple rooms, triggering javascript/jQuery to dynamically create additional input ...

Implementation issue with Hashids library in Vue.js causing functionality hiccups

I'm having trouble getting the library hashids to cooperate with vue.js The method I would like to use is: <template> <div class="container"> {{ hashids.encode('1') }} </div> </template> <script& ...

How to modify values in a JSON array using JavaScript

Currently, I am facing an issue with displaying dates properly on the x-axis of a graph created using Highcharts. To solve this problem, I need to parse the dates from the JSON response. Despite my attempts to manipulate the JSON date, I have not been able ...

What is the best method to retrieve the title from an array using javascript?

I am working with a JSON object containing information on thousands of students. I have converted this JSON object into an array, and below is an example of how one array looks: [ 'Alex', { id: '0.0010733333111112', grade: &apos ...

Ways to incorporate a scroll feature and display an iframe using JQuery

Is there a way to animate the appearance of hidden iframes one by one as the user scrolls down the website using jQuery? I have come across some solutions, but they all seem to make them appear all at once. I'm looking for a way to make the iframes s ...

Is there a way to verify the functionality of DigitalOcean features using doctl before transitioning to a live environment?

As a newcomer to using DigitalOcean Functions, I have set up some JavaScript namespaces using the doctl serverless command. My main query is regarding testing DigitalOcean functions locally before deploying them to production. Is there a way to do this wit ...

Incorporating Node.JS variables within an HTML document

After building a simple site using Express, I discovered that Node.js variables can also be used with Jade. exports.index = function(req, res){ res.render('index', { title: 'Express' }); }; This is the code for the index file: ext ...

Saving the output of a function in Javascript/NodeJS to a variable

I've been attempting to save the output of a function into a variable, but it's not working as expected. I've exhausted all my ideas and possibilities. Perhaps I'm just making a silly mistake :D I'm working with NodeJS, using expre ...

Caught in the midst of a JSON update conundrum

I need some help with my JavaScript/JSON coding. I have a script that loads JSON data and displays it on an HTML page. Now, I want to know how I can update this data. Specifically, I want the script to update the location of the person when a button is cli ...

Highchart Javascript integration with Klipfolio data

I have been working on a project that involves creating a Gantt chart using Highchart in Klipfolio. I have successfully designed the gantt chart using Klipfolio's Html component, but I am facing challenges with integrating the data properly. Custom K ...

Creating a JSON object in AngularJS is a simple and straightforward process

Is it a good practice to create a JSON object in AngularJS this way? Or is there a better solution to achieve the desired format? Edit question: I am trying to create an object in JSON format as shown below. I have written the code but facing difficulty ...

safely sending different form inputs in a Ruby on Rails form

Within this snippet, you'll find a part of a table with a form. Each row has its own submit button, which means users have to click on each one individually. I'm currently working on changing this so there is just one button for submission. < ...

Is there a way to restrict input to numeric characters exclusively in Swal Sweetalert?

I'm wondering how I can restrict input to only numeric values in this section. { title: "Dollar Amount?", text: "How much is your Dollar Question worth?", inputPlaceholder: "Enter Amount" } I'm currently utilizing a Sweetalert p ...

Display the splash screen while the app completes the loading process

Upon testing my app, I realized that it takes too long to load all the content on startup without a splash screen. To solve this issue, I decided to implement a splash screen that will be displayed until the loading process is complete. I came across an ex ...

Is it possible to eliminate the css class prefix when using Modular css in React?

When working with React & NextJS, I have observed that my modular SCSS files are automatically prefixing my classnames with the name of the file. Is there a way to turn off this feature as it is making the DOM structure difficult to interpret? For instanc ...

What is the process for combining two services into a single, unified service?

In my Angular 8 application, I have a service structured like this: export class ProfileUserService { user$ = this.authService.loginStatus().pipe(take(1)); constructor(private profileService: ProfileService, private authService: AuthService) {} ge ...

Issue with Ajax post redirection back to original page

I'm facing an issue with my ajax call where I send multiple checkbox values to a php file for processing and updating the database. The post request and database updates are successful, but the page doesn't return to the calling php file automati ...

Having difficulty accessing `props` in a React JS and Next JS application

Currently, I am developing a React application that utilizes Server Side Rendering. In this project, I am using React Js and Next Js as my primary framework. My goal is to retrieve initial props using the getServerSideProps method by consulting the documen ...

Transferring data between a ComponentPortal within an Angular CDK

After attempting to implement the method described in a Stack Overflow thread on Angular CDK: How to set Inputs in a ComponentPortal, I've encountered issues with the deprecated PortalInjector without clear guidance on what to use instead. The depreca ...