Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Referencing an XML Data Structure?

Status
Not open for further replies.

davem99

Programmer
May 19, 2003
74
US
Where I'm hooking into a site and want to access an XML structure through the DOM.

Can anyone tell me where XML lives within the DOM?

thanks!
 
Not quite sure what you mean by your question, but to display an XML document within an HTML document you will need to retrieve the XML document first and then parse it for the required tags.

The main guts of the retrieval function is:
Code:
...
function xmlImportXML(pageName) {
	// Import the XML into the webpage
	if (document.implementation && document.implementation.createDocument) {
		xmlDoc = document.implementation.createDocument("", "", null);
		// process the document once it has loaded
		xmlDoc.onload = xmlCreateArray();
	} else if (window.ActiveXObject) {
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		// process the document once it has loaded
		xmlDoc.onreadystatechange = function () {if (xmlDoc.readyState == 4) xmlCreateArray()};
 	} else {
		alert('Your browser can\'t handle this script');
		return;
	}

	xmlResults.innerHTML=&quot;<span class='message'>Please wait... loading</span>&quot;;
	xmlDoc.load(pageName);
}
...

In your BODY section:
Code:
...
<div id=&quot;xmlResults&quot;></div>
<script language=&quot;JavaScript&quot;>
<!--
xmlImportXML(&quot;../xml/ac7.3.1-annualresults.xml&quot;);
//-->
</script>
...

Then, once you have a fully loaded document call some other functions (the first called xmlCreateArray) to extract the information.

Is this the sort of thing you were after?

Pete.


Web Developer / CMS (Aptrix / LWWCM) Specialist
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Hey Pete - thanks!!!

Here's the scenario - I'm automating a highly repetitive task for a client. It involves logging into a website, performing a search and grabbing the results.

I have a VB app that hosts an IE component. My app logs in and fills search criteria, clicks buttons etc...A lot of the data I need I can get by grabbing document.body.frames(x).frames(y).innerhtml and so on.

My problem arises on one page where the site renders data using an HTC component that sucks data from a DB into an XML structure.

So....I have the IE control and all if it's data...what I'm struggling with is how to reference the XML structure that is being used to holkd this data?!?!?

Thanks again - any further pointers greatly appreciated!

 
You can reference the XML document, just like any DOM. Have a read of:

Example:
Code:
XML Document - save it as &quot;states.xml&quot;:
<States> 
   <State ref=&quot;FL&quot;> 
      <name>Florida</name> 
      <capital>Tallahassee</capital> 
   </State> 
   <State ref=&quot;IA&quot;> 
      <name>Iowa</name> 
      <capital>Des Moines</capital> 
   </State> 
</States>

HTML Document to extract the information:
<html>
<head>
	<title>XML Example</title>
</head>
<body>
<script>
var xml_doc = new ActiveXObject(&quot;Microsoft.XMLDOM&quot;); 
xml_doc.async = false; 
xml_doc.load(&quot;states.xml&quot;); 

var f_child = xml_doc.documentElement.firstChild.text;
document.write(&quot;First: &quot; + f_child);
f_child = xml_doc.documentElement.firstChild.nextSibling.text; 
document.write(&quot;<br>Second: &quot; + f_child);
</script>
</body>
</html>

You should get:
First: Florida Tallahassee
Second: Iowa Des Moines

Does this help?

Pete.


Web Developer / Aptrix CMS (LWWCM) Specialist
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Hi Pete - thanks again for the extra info.

The thing that I'm struggling with here (and it may very well be stupidity on my part!!) is that I'm outside of the browser. Because I'm working in VB talking to a browser control in my app, I don't know how to even find the XML.

I know within HTML how I could grab the data (I think), but I'm trying to figure out how, using the Browser.Document or something similar that I can find THEIR variables that hold the XMLDOM.

Am I explaing this clearly?
 
The Microsoft website seems to have a lot of example information... I've had a brief look, but you might be able to find what you are looking for. Start here - which contains links for C/C++, VBScript and JScript:

I'll dig around aswell.

Pete.


Web Developer / Aptrix CMS (LWWCM) Specialist
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Some great resources, Pete! This will give me what I need on the VB side of the fence.

But....(there's always a but!)....how do I make my little VB app talk to the XML structure that lives within a series of JS files within a WebBrowser component in my VB app?

Thanks again, Pete - any more ideas much appreciated!!!

 
I think I'm finally starting to understand what you are trying to do. I don't think I've done anything like you're trying to do before... but (and as you say, there's always a but) I'll have a go.

Do you have an example webpage, or a snippet of the contents of the JS file containing the XML structure or it's call to the XML document?

I'm still a little confused as to how the XML structure is returned to be displayed in the browser. Is the JS file retrieving the XML document, as I have described above in one of my examples?

If this is the case, then you could parse the HTML Document, to sequentially retrieve each of the JS files, and then within each of the JS files perform as string search for the regular expression sequence containing &quot;
Code:
.load(
&quot; as in:
Code:
xmlDoc.load(pageName);

The first part would be your variable containing the XML document.

Am I even getting slightly warm yet? ;-)

Pete.


Web Developer / Aptrix CMS (LWWCM) Specialist
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Hi Pete - believe me.....I've posted this on so many boards and you are smoking when it comes to getting close!!!

While the site owners give us their blessing to automates this process, I'm not sure they'd like their code posted in a puiblic forum.

I'd be happy to send the JS scripts to you directly and then the results of that can be posted here so that the world can bow before you :)

To answer your other question, yes they seem to grab the structure in a way similar to the example you gave above. And don't forget that I can call their code from my VB world too.

Let me know!
 
&quot;... yes they seem to grab the structure in a way similar to the example you gave above. And don't forget that I can call their code from my VB world too...&quot;

By the sounds of it then, you have a couple of options:
1. Find out what URL or pagename is being requested within the JavaScript and load that into an XML document variable within VB - the Microsoft site should show you how to load the XML file.
2. Access their JavaScript variable directly (not quite sure how to do this via VB)

To help you locate the XML document name, you can access the HEAD section of any webpage using a call to:
Code:
document.getElementsByTagName('head')

You can try this in any browser (IE) by entering the following into the Address bar:
Code:
javascript:alert(document.getElementsByTagName('head')[0].innerHTML);

Since &quot;getElementsByTagName&quot; returns an Array, so you need to reference the first element - assuming there is only 1 head declaration. By looping through each element within the head, you can target the &quot;SCRIPT&quot; tag directly and try to access the variable name(s). Alternatively, (as I mentioned before) you could simply do a search for a string.

If they seem to grab the structure in a way similar to the example above, then even by looking at the code, I don't think it will help.

This is (sort of) sounding like what I am planning to do with an ASP setup I have - to export all of my ASP files to static HTML and rewrite the URLs/filenames. I can maintain the site much more easily, and host it anywhere since it doesn't require an IIS server to run. :)

With that said, I'm not quite sure HOW to get the XML document name to retrieve, other than parsing for it.

Pete.


Web Developer / Aptrix CMS (LWWCM) Specialist
w: e: Pete.Raleigh(at)lclimited.co.uk
 
OK, Mr. WartookMan!

I'm getting closer. The HTC component I'm trying to get to lives in:

Document.frames(1).frames(1).document.namespaces(&quot;IRC&quot;)

and the code looks like:

<TR>
<TD width=&quot;100%&quot; height=&quot;100%&quot;>
<!-- Grid -->
<?IMPORT NAMESPACE = IRC IMPLEMENTATION = &quot;someoldGrid.htc&quot; />
<IRC:someoldGrid id=SomeGridControl
onreadystatechange=InitGrid()
style=&quot;OVERFLOW-Y: hidden; OVERFLOW-X: hidden; WIDTH: 100%; HEIGHT: 100%&quot;
name=&quot;GridControl&quot; expando=&quot;true&quot;
onColumnRequest=&quot;parent.search(event.objColReq, this)&quot;
onDataRequest=&quot;parent.searchGetRow(this, event.aIndices, event.sCallBackScript)&quot;
onError=&quot;GridError()&quot;
onAllSelected=&quot;AllSelected()&quot;
onExpandRow=&quot;ExpandIt(event.pExpandSpan, event.iRow, event.sSysID)&quot;
onRowChecked=&quot;RowClicked()&quot;
onGridReady=&quot;FilterIt()&quot;>
</IRC:SmartGrid>
</TD>
</TR>

I've looked at the HTC file that is implemented by the above and it exposes several properties & methods.

One property is &quot;myGridXML&quot; which presumably returns the XML data structure.

BUT, I can't figure out the syntax to get to it. I'd assume that:

Document.frames[1].frames[1].SomeGridControl.myGridXML

would work but it doesn't :(

Any clues?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top