XML nodes stepthrough

Just starting out? Need help? Post your questions and find answers here.
User avatar
purebuilt
User
User
Posts: 46
Joined: Sun Dec 17, 2006 5:30 pm
Location: Milwaukee, WI, USA

XML nodes stepthrough

Post by purebuilt »

I need some help with the XML. I am trying to extract text from an XML document I load into a string.

Code: Select all

<section id="sm51_title_bar_sql" class="sql">  /* SM51 Title Bar Query */
  SELECT title       AS sm51_title_bar,
         description AS sm51_desc,
         status      AS sm51_status
  FROM rep.sm51_title_bar;
</section>

<section id="sm51_data_sec_REPEAT_sql" class="sql">  /* Repeating Data Section Query */
  SELECT backup_type,last_backup,status AS backup_status 
  FROM rep.sm51_backups_view
  WHERE code LIKE '%++code++%' AND sid = 'PRD';
  ORDER BY backup_type;
</section>

<section id="sm51_footer_sql" class="sql">  /* SM51 Comments Query */
  SELECT comment_label AS sm51_comment_label,
         comments      AS sm51_comments
  FROM rep.report_details
  WHERE code LIKE '%++code++%' AND sid = 'PRD';
</section>
I am not having a lot of success with stepping through the nodes and gathering the IDs and Text. Can anyone help me? Also, why do many of the examples use pointers with XML.

I am very new at XML parsing here.

Thanks
- DB
User avatar
Kiffi
Addict
Addict
Posts: 1486
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: XML nodes stepthrough

Post by Kiffi »

Code: Select all

Define XML.s
Define oNode

XML = "<sections>" + 
      "<section id='sm51_title_bar_sql' class='sql'>  /* SM51 Title Bar Query */" + 
      "  SELECT title       AS sm51_title_bar," + 
      "         description AS sm51_desc," + 
      "         status      AS sm51_status" + 
      "  FROM rep.sm51_title_bar;" + 
      "</section>" + 
      "" + 
      "<section id='sm51_data_sec_REPEAT_sql' class='sql'>  /* Repeating Data Section Query */" + 
      "  SELECT backup_type,last_backup,status AS backup_status " + 
      "  FROM rep.sm51_backups_view" + 
      "  WHERE code LIKE '%++code++%' AND sid = 'PRD';" + 
      "  ORDER BY backup_type;" + 
      "</section>" + 
      "" + 
      "<section id='sm51_footer_sql' class='sql'>  /* SM51 Comments Query */" + 
      "  SELECT comment_label AS sm51_comment_label," + 
      "         comments      AS sm51_comments" + 
      "  FROM rep.report_details" + 
      "  WHERE code LIKE '%++code++%' AND sid = 'PRD';" + 
      "</section>" + 
      "</sections>"

If ParseXML(0, XML) And XMLStatus(0) = #PB_XML_Success
	
	oNode = XMLNodeFromPath(MainXMLNode(0), "section")
	
	While oNode
		
		Debug "id: "    + GetXMLAttribute(oNode, "id")
		Debug "class: " + GetXMLAttribute(oNode, "class")
		Debug "text: "  + GetXMLNodeText(oNode)
		Debug "----"
		
		oNode = NextXMLNode(oNode)
		
	Wend
	
	FreeXML(0)
	
Else
	Debug XMLError(0)
EndIf
Greetings ... Peter
Hygge
User avatar
purebuilt
User
User
Posts: 46
Joined: Sun Dec 17, 2006 5:30 pm
Location: Milwaukee, WI, USA

Re: XML nodes stepthrough

Post by purebuilt »

This is great, but is there a way to step through an xml document looking just for <section> tags (or others) and get the attributes from them?

I am trying to use an xhtml document as a template and imbed the sql queries to fill out the data.

Thanks
- DB
User avatar
Kiffi
Addict
Addict
Posts: 1486
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: XML nodes stepthrough

Post by Kiffi »

this code read all section-Nodes (regardless of the depth):

Code: Select all

EnableExplicit

Procedure ReadChildNodes(Node, NodeName.s)
	
	Protected ChildNode
	
	ChildNode = ChildXMLNode(Node)
	
	While ChildNode <> 0
		If GetXMLNodeName(ChildNode) = NodeName 
			
			Debug "id: "    + GetXMLAttribute(ChildNode, "id")
			Debug "class: " + GetXMLAttribute(ChildNode, "class")
			Debug "text: "  + GetXMLNodeText(ChildNode)
			Debug "----"			
			
		EndIf
		If XMLChildCount(ChildNode)
			ReadChildNodes(ChildNode, NodeName)
		EndIf
		ChildNode = NextXMLNode(ChildNode)
	Wend
	
EndProcedure

#XML = 0

Define XML.s = "<sections>" + 
               " <subsection>" + 
               "  <subsubsection>" + 
               "   <section id='sm51_title_bar_sql' class='sql'>  /* SM51 Title Bar Query */" + 
               "  SELECT title       AS sm51_title_bar," + 
               "         description AS sm51_desc," + 
               "         status      AS sm51_status" + 
               "  FROM rep.sm51_title_bar;" + 
               "   </section>" + 
               "  </subsubsection>" + 
               " </subsection>" + 
               " <section id='sm51_data_sec_REPEAT_sql' class='sql'>  /* Repeating Data Section Query */" + 
               "  SELECT backup_type,last_backup,status AS backup_status " + 
               "  FROM rep.sm51_backups_view" + 
               "  WHERE code LIKE '%++code++%' AND sid = 'PRD';" + 
               "  ORDER BY backup_type;" + 
               " </section>" + 
               " <section id='sm51_footer_sql' class='sql'>  /* SM51 Comments Query */" + 
               "  SELECT comment_label AS sm51_comment_label," + 
               "         comments      AS sm51_comments" + 
               "  FROM rep.report_details" + 
               "  WHERE code LIKE '%++code++%' AND sid = 'PRD';" + 
               " </section>" + 
               "</sections>"

Define MainNode

If ParseXML(#XML, XML)
	If XMLStatus(#XML) = #PB_XML_Success
		MainNode = MainXMLNode(#XML)
		If MainNode
			ReadChildNodes(MainNode, "section")
		EndIf
	Else
		MessageRequester("XPathHelper", "Error: " + XMLError(#XML) + #CRLF$ + "Line: " + Str(XMLErrorLine(#XML)) + #CRLF$ + "Position: " + Str(XMLErrorPosition(#XML)))
	EndIf
	FreeXML(#XML)
EndIf	
Greetings ... Peter
Hygge
Post Reply