New topic: 

XML Parsing

<http://forums.realsoftware.com/viewtopic.php?t=38050>

         Page 1 of 1
   [ 3 posts ]                 Previous topic | Next topic          Author  
Message        tudorH          Post subject: XML ParsingPosted: Thu Mar 10, 
2011 7:18 pm                         
Joined: Thu Feb 03, 2011 5:44 pm
Posts: 21                Hi

I'm trying to parse an XML string from one text area and find the number of 
nodes + the text from inside the XML tags...I am trying to get the code from 
the RealBasic documentation working but am having no luck, below is my code:
Code:  Dim xdoc as XmlDocument
  Dim node as XMLNode
  Dim i, count as Integer
  Dim out as String
  
  'dim f as new FolderItem("test.txt")
  
  // create a new document
  xdoc = New XmlDocument
  
  xdoc.PreserveWhitespace = True
  
  // load and parse the xml in the TextArea, parse_InputText
  xdoc.LoadXml(parse_input.text) 
  
  // lets first do some simple, static traversing of the xml
  // this doesn't do any error checking to make sure stuff exists
  // so if the document changes, it will break.
  count = xdoc.DocumentElement.childCount
  out = "Number of Nodes: " + Str(count)  //number of Person nodes
  out = out + EndOfLine
  
  // childNodes are 0 based.
  For i = 0 to count - 1
  
  node = xdoc.DocumentElement.Child(i)
  
  // this gets the textnode of the <firstname> node
  out = out + node.FirstChild.FirstChild.Value
  // this gets the textnode of the <lastname> node
  out = out + " " + node.LastChild.FirstChild.Value
  out = out + EndOfLine
  
  next
  
  //display results in the TextArea parse_output
  parse_output.text = out


when I run the code and enter my XML string, it exits at the "out = out + 
node.FirstChild.FirstChild.Value" line, which is where the error is...I'm not 
sure how to find out what the error is?

below is the XML string I am using
Code:<RATE>
<ISFLAGGED>*</ISFLAGGED>
<FLAGURL>flags/CAD.JPG</FLAGURL>
<ISO>CAD</ISO>
<NAME>Dollars</NAME>
<COUNTRY>Canada</COUNTRY>
<WEBUY>1</WEBUY>
<WESELL>1</WESELL>
<INVBUY>1</INVBUY>
<INVSELL>1</INVSELL>
</RATE>


thanks for any help   
                             Top                 timhare          Post subject: 
Re: XML ParsingPosted: Thu Mar 10, 2011 8:30 pm                         
Joined: Fri Jan 06, 2006 3:21 pm
Posts: 9429
Location: Portland, OR  USA                2 problems.  First, 
PreserveWhiteSpace = true killed it for me reading from a textarea.  Try 
setting it to false.

Second, you're starting too low in the hierarchy.  xdoc.DocumentElement in this 
case is the <RATE> node, so xdoc.DocumentElement.Child(0) is the <ISFLAGGED> 
node.  It has a FirstChild, but there is no FirstChild.FirstChild.   
                             Top                timhare          Post subject: 
Re: XML ParsingPosted: Thu Mar 10, 2011 8:58 pm                         
Joined: Fri Jan 06, 2006 3:21 pm
Posts: 9429
Location: Portland, OR  USA                The discrepancies between your 
example and the documentation is that you are using a snippet of xml, not a 
true xml document.  Therefore, xdoc.DocumentElement isn't really defined, so 
it's coming back with the first node.   
                             Top             Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 3 posts ]      
-- 
Over 1500 classes with 29000 functions in one REALbasic plug-in collection. 
The Monkeybread Software Realbasic Plugin v9.3. 
http://www.monkeybreadsoftware.de/realbasic/plugins.shtml

[email protected]

Reply via email to