XML DOM నోడ్ జాబితా

getElementsByTagName() methods and childNodes attribute, it returns a list of nodes.

DOM నోడ్ జాబితా

When using childNodes or getElementsByTagName() When using properties or methods such as

The NodeList object represents a list of nodes, with the order same as in XML.

Nodes in the node list can be accessed by index starting from 0.

The following figure represents books.xml Node list of the <title> element:

DOM నోడ్ జాబితా

Assuming "books.xml" has been loaded into the variable xmlDoc.

This code snippet returns the node list of the title element in "books.xml":

x = xmlDoc.getElementsByTagName("title");

After executing the above statements, x becomes a NodeList object.

ఈ కోడ్ బిందు జాబితా (x) లోని మొదటి <title> మూలకం టెక్స్ట్ ను తిరిగి చెపుతుంది:

ప్రకారం

var txt = x[0].childNodes[0].nodeValue;

స్వయంగా ప్రయత్నించండి

ఈ వాక్యం అమలు అయిన తర్వాత, txt = "యాశే టాన్ చీక్కరి".

నోడ్ జాబితా పొడవు

NodeList బిందు స్వయంచాలకంగా తాజాగా ఉంటుంది. నోడ్లను తొలగించినా లేదా జోడించినా, జాబితా స్వయంచాలకంగా నవీకరిస్తుంది.

నోడ్ జాబితా యొక్క length అంశాలు జాబితాలో నోడ్ల సంఖ్యలు.

ఈ కోడ్ "books.xml" లోని <title> మూలకాల సంఖ్యను తిరిగి చెపుతుంది:

x = xmlDoc.getElementsByTagName('title').length;

పై వాక్యం అమలు అయిన తర్వాతx విలువ 5 ఉంది.

పొడవు విలువ యొక్క

ఈ కోడ్ ప్రామాణికంగా నోడ్ జాబితా పొడవును ఉపయోగిస్తుంది మరియు జాబితాలోని అన్ని మూలకాలను చుట్టూ పరిగణిస్తుంది. length అంశాల లోపల చుట్టూ పరిగణించడానికి

ప్రకారం

x = xmlDoc.getElementsByTagName('title');
xLen = x.length;
for (i = 0; i <xLen; i++) {
    txt += x[i].childNodes[0].nodeValue) + " ";
}

స్వయంగా ప్రయత్నించండి

ఉదాహరణ వివరణం:

  1. హాస్యం books.xml ప్రామాణికంగా లోడు చేయబడింది xmlDoc మధ్య
  2. వివరణ ను నిర్వహించడానికి x వ్యవహరణను సెట్ చేస్తారు
  3. ప్రామాణికంగా <title> మూలకం నుండి టెక్స్ట్ నోడ్ బిందు విలువను పొందుతుంది

DOM అంశ జాబితా (named node map)

మూలక అంశాలు attributes అంశాలు అంశ నోడ్ బిందు జాబితాను తిరిగి చెపుతుంది.

ఇది పేరుదారు నోడ్ మాప్ (named node map) అని పిలుస్తారు, ఇది నోడ్ జాబితానికి వంటిది, కానీ పద్ధతులు మరియు అంశాలు వివిధంగా ఉంటాయి.

అంశ జాబితా కూడా తాజాగా ఉంటుంది. అంశాలను తొలగించినా లేదా జోడించినా, జాబితా స్వయంచాలకంగా నవీకరిస్తుంది.

ఈ కోడ్ "books.xml" లోని మొదటి <book> మూలకం అంశ నోడ్ బిందు జాబితాను తిరిగి చెపుతుంది:

x = xmlDoc.getElementsByTagName('book')[0].attributes;

పై కోడ్ అమలు అయిన తర్వాత,x.length అంశం లెంకతను సమానంగా ఉంచవచ్చు, దానిని ఉపయోగించవచ్చు x.getNamedItem() ఒక అంశ నోడ్ బిందును తిరిగి చెపుతుంది.

ఈ కోడ్ ప్రామాణికంగా మొదటి పుస్తకం యొక్క "category" అంశం విలువను మరియు అంశం లెంకతను పొందుతుంది:

ప్రకారం

x = xmlDoc.getElementsByTagName("book")[0].attributes;
txt = x.getNamedItem("category").nodeValue + " " + x.length;

స్వయంగా ప్రయత్నించండి

ఉదాహరణ వివరణం:

  1. హాస్యం books.xml లోకి లోడ్ చేయబడింది xmlDoc మధ్య
  2. మొదటి <book> ఎలిమెంట్ అట్రిబ్యూట్ జాబితాను సేవ్ చేయడానికి x వేరియబుల్ సెట్ చేయండి
  3. "category" అట్రిబ్యూట్ విలువను మరియు అట్రిబ్యూట్ జాబితా పొడవును పొందండి