Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Reading Node from XML Document


This question is answered.


Permlink Replies: 4 - Last Post: Jul 20, 2017 11:55 AM Last Post By: Derek Moore
Derek Moore

Posts: 3
Registered: 8/20/17
Reading Node from XML Document  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 20, 2017 9:02 AM
I'm trying to read an XML page from the web, and access a specific node. Should be fairly simple, but first time using these XML commands and I'm clearly missing something.

With the code below, I'm able to read the XML page correctly into a string. What I'm failing to grasps is how to load that string into the XMLDocument format for it to be parsed correctly. (?) Currently I get an Invalid Pointer Operation error.

	String serverurl = "https://www.w3schools.com/xml/cd_catalog.xml";
 
	TMemoryStream *XML = new TMemoryStream;
	AnsiString answer="";
 
	HTTPObj = new TIdHTTP(NULL);
	try
	{
		SSLHandle = new TIdSSLIOHandlerSocketOpenSSL(NULL);
		HTTPObj->IOHandler = SSLHandle;
		HTTPObj->ConnectTimeout = 2000;
 
		HTTPObj->Get(serverurl,XML);
		XML->Position = 0;
		answer=ReadStringFromStream(XML);  // Confirmed the string 'answer' at this point contains all the XML text.
 
		XMLDocument1 = new TXMLDocument(NULL);
		XMLDocument1->Active=TRUE;
		XMLDocument1->LoadFromXML(answer);
 
		_di_IXMLNode result1 = XMLDocument1->DocumentElement->ChildNodes->GetNode(0);
		answer = result1->ChildNodes->Nodes[WideString("TITLE")]->Text;
 
		MainWindow->IPBlock->Text = answer;
		HTTPObj->Free();
	}
	catch (...)
	{
		HTTPObj->Free();
	}


Would appreciate suggestions as for what I'm missing here. Thanks.

I'm using C++ Builder XE6
Antonio Estevez

Posts: 665
Registered: 4/12/00
Re: Reading Node from XML Document
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 20, 2017 10:31 AM   in response to: Derek Moore in response to: Derek Moore
El 20/07/2017 a las 18:08, Derek Moore escribió:
I'm trying to read an XML page from the web, and access a specific node. Should be fairly simple, but first time using these XML commands and I'm clearly missing something.

With the code below, I'm able to read the XML page correctly into a string. What I'm failing to grasps is how to load that string into the XMLDocument format for it to be parsed correctly. (?) Currently I get an Invalid Pointer Operation error.

Would appreciate suggestions as for what I'm missing here. Thanks.

I'm using C++ Builder XE6

The next code shows how you can search data through the xml content:

	String serverurl = "https://www.w3schools.com/xml/cd_catalog.xml";
 
	_di_IXMLDocument XmlDoc;
 
	TIdHTTP *HTTPObj = new TIdHTTP(NULL);
	try
	{
		TIdSSLIOHandlerSocketOpenSSL *SSLHandle = new TIdSSLIOHandlerSocketOpenSSL(HTTPObj);
		HTTPObj->IOHandler = SSLHandle;
		HTTPObj->ConnectTimeout = 2000;
 
		TMemoryStream *XML = new TMemoryStream;
		try
		{
			HTTPObj->Get(serverurl, XML);
			XML->Position = 0;
 
			XmlDoc= NewXMLDocument();
			XmlDoc->LoadFromStream(XML);
		}
		__finally
		{
			delete XML;
		}
	}
	__finally
	{
		delete HTTPObj;
	}
 
	if (XmlDoc)
	{
		_di_IXMLNode CatalogNode= XmlDoc->DocumentElement;
		_di_IXMLNodeList CatalogList= CatalogNode->ChildNodes;
		for  (int Index= 0; Index< CatalogList->Count; ++Index)
		{
			_di_IXMLNodeList CDItem= CatalogList->Nodes[Index]->ChildNodes;
 
			String Title= CDItem->Nodes[String(L"TITLE")]->Text;
			String Artist= CDItem->Nodes[String(L"ARTIST")]->Text;
 
			// Or
			//String Title= CDItem->FindNode(L"TITLE")->Text;
			//String Artist= CDItem->FindNode(L"ARTIST")->Text;
 
			// Use Title, Artist, ... as needed
				
		}
	}
Derek Moore

Posts: 3
Registered: 8/20/17
Re: Reading Node from XML Document  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 20, 2017 11:18 AM   in response to: Antonio Estevez in response to: Antonio Estevez
Antonio Estevez wrote:
The next code shows how you can search data through the xml content:

Perfect! Thank you.

Another question I have is about reading different formats. The XML page I need to read is formatted like this:
<status>
	<value type="VoltageA" value="12"/>
	<value type="VoltageB" value="5"/>
	<value type="CurrentA" value="468"/>
	<value type="CurrentB" value="1149"/>
</status>


Is this even valid XML formatting? And if so, how do I treat the nodes? Is each 'type' a child node of 'status', and then the 'value' is a child node of that type?
Antonio Estevez

Posts: 665
Registered: 4/12/00
Re: Reading Node from XML Document
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 20, 2017 11:31 AM   in response to: Derek Moore in response to: Derek Moore
El 20/07/2017 a las 20:18, Derek Moore escribió:
Antonio Estevez wrote:
The next code shows how you can search data through the xml content:

Perfect! Thank you.

Another question I have is about reading different formats. The XML page I need to read is formatted like this:
<status>
	<value type="VoltageA" value="12"/>
	<value type="VoltageB" value="5"/>
	<value type="CurrentA" value="468"/>
	<value type="CurrentB" value="1149"/>
</status>


Is this even valid XML formatting? And if so, how do I treat the nodes? Is each 'type' a child node of 'status', and then the 'value' is a child node of that type?

Yes, they are valid xml nodes.

    String Type= ValueNode->Attributes[L"type"];
    int Value= ValueNode->Attributes[L"value"];
Derek Moore

Posts: 3
Registered: 8/20/17
Re: Reading Node from XML Document  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 20, 2017 11:55 AM   in response to: Antonio Estevez in response to: Antonio Estevez
Antonio Estevez wrote:
Yes, they are valid xml nodes.

    String Type= ValueNode->Attributes[L"type"];
    int Value= ValueNode->Attributes[L"value"];

Perfect again. Thanks for the very quick and accurate responses! Very appreciated.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02