As was foretold, we've added advertisements to the forums! If you have questions, or if you encounter any bugs, please visit this thread: https://forums.penny-arcade.com/discussion/240191/forum-advertisement-faq-and-reports-thread/
Options

Parsing XML in Javascript

RMS OceanicRMS Oceanic Registered User regular
edited December 2015 in Help / Advice Forum
So I have an XML document that looks like this:
"doc.xml"
<Response>
   <IrrelevantValue>Hi</IrrelevantValue>
   <IrrelevantValue2>Hi2</IrrelevantValue2>
   <AttributeObj>
      <Name>Text</Name>
      <Value>Text<Value>
   </AttributeObj>
   <AttributeObj>
      <Name>Text</Name>
      <Value>Text<Value>
   </AttributeObj>
   <AttributeObj>
      <Name>Text</Name>
      <Value>Text<Value>
   </AttributeObj>
</Response>

I want to extract all the AttributeObjs and parse the value of their Name and Value tags. I've started with something like this:
var AttributeList = doc.getElementsByTagName('AttributeObj');
print(AttributeList.length); //3
for (int i = 0; i < AttributeList.Length; i++
{
   var Name = AttributeList[i].getElementsByTagName('Name').item(0).firstChild.data;
   var Value = AttributeList[i].getElementsByTagName('Value').item(0).firstChild.data;
   //Do a bunch of stuff with Name and Value
}

However my diagnostic logging shows that Name and Value are always blank. Does anyone know what I'm doing wrong?

RMS Oceanic on

Posts

  • Options
    MorkathMorkath Registered User, __BANNED USERS regular
    edited December 2015
    Too lazy to open VS atm, but I believe you should be using double quotes for the tag names.
    var Name = AttributeList[i].getElementsByTagName("Name").item(0).firstChild.data;
    

    e:
    For further info (and i'm not super literate in javascript so this may not be 100% for you, but);
    ' is typically used to specify characters, while " is for strings.

    Morkath on
  • Options
    P10P10 An Idiot With Low IQ Registered User regular
    I think your logic is sound but you have syntax errors - based on the snippet you posted.
    the for loop should be
    for (i = 0; i < AttributeList.length; i++) {
           //loop stuff
    }
    
    javascript doesn't know what an int is, length needs to be lower case and you need the end parenthesis.
    jsfiddle

    Shameful pursuits and utterly stupid opinions
  • Options
    admanbadmanb unionize your workplace Seattle, WARegistered User regular
    Morkath wrote: »
    Too lazy to open VS atm, but I believe you should be using double quotes for the tag names.
    var Name = AttributeList[i].getElementsByTagName("Name").item(0).firstChild.data;
    

    e:
    For further info (and i'm not super literate in javascript so this may not be 100% for you, but);
    ' is typically used to specify characters, while " is for strings.

    Javascript actually doesn't care, though JSON cares a lot.

  • Options
    admanbadmanb unionize your workplace Seattle, WARegistered User regular
    edited December 2015
    @RMS Oceanic -- this may be a transcription error, but none of your <Value> tags are closed correctly. That could be what's blowing up your code.

    admanb on
Sign In or Register to comment.