Anton Hoerl
Anton Hoerl

Reputation: 189

Getting textContent of XML node in Javascript

I have XML:

let rowTest = <?xml version="1.0" encoding="UTF-8"?>
<queryResponse Name="Checked In Guests">
   <rows>
      <row>
         <fields>
            <field name="PartyName" />
            <Notes>Rosen bestellen.</Notes>
            <field name="AccompanyingGuest" name1="Test" name2="" name3="test" BedGuestNum="82259">1418</field>
         </fields>
      </row>
   </rows>
</queryResponse>

I want to get the text in the Notes Tag. But somehow I am able to do that. My try:

 row = parser.parseFromString(rowTest, "text/xml");
 let notes = row.getElementsByTagName("Notes");
 let noteText = notes.textContent;
 console.log(noteText);

undefined

Upvotes: 0

Views: 191

Answers (1)

Vodka
Vodka

Reputation: 36

Try this code.

let rowTest = '<?xml version="1.0" encoding="UTF-8"?> <queryResponse Name="Checked In Guests"> <rows> <row> <fields> <field name="PartyName" /> <Notes>Rosen bestellen.</Notes> <field name="AccompanyingGuest" name1="Test" name2="" name3="test" BedGuestNum="82259">1418</field> </fields> </row> </rows> </queryResponse></xml>';
 parser = new DOMParser();
 row = parser.parseFromString(rowTest, "text/xml");
 let notes = row.getElementsByTagName("Notes")[0];
 let noteText = notes.textContent;
 console.log(noteText);

Upvotes: 1

Related Questions