Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2006
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts

    retrieving HTML inside XML

    I am working on a script that retrieves a list of jargon terms from an XML file, finds the term in a HTML document and then wraps the terms it finds in links that open a div wit ha definition of the term embedded. Inside the XML there are HTML fragments that I want to insert. My problem is how to easily extract the HTML from within the XML for use. Here's an example of the XML:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <jargon>
    	<jargonterm id="3lcd" term="3LCD">
    		<description>
    			<p>
    				3LCD is the most widely used projection technology system in the world. This is how 3LCD technology works: white light is split into red, green, and blue using two mirrors that transmit light with a certain wavelength.
    			</p>
    			<p>
    				Each colour is then passed through a dedicated LCD, before being combined with the other colors in a prism. The image is then ready to be projected onto the screen.
    			</p>
    		</description>
    	</jargonterm>
    	<jargonterm id="ansi_Lumens" term="ANSI Lumens">
    		<description>
    			<p>
    				ANSI lumens is a measurement of the overall brightness of a projector. Because the centre of a projected image is brighter than the corners, ANSI lumens is the most accurate representation of the image brightness. ANSI lumens are calculated by dividing a square meter image into 9 equal rectangles, measuring the lux (or brightness) reading at the centre of each rectangle, and averaging these nine points.
    			</p>
    		</description>
    	</jargonterm>
    </jargon>
    Here's the code that does the link insersion and which also needs to extract the HTML fragments from inside the <description> nodes. as you can probably guess it's fired by an AJAX event.

    PHP Code:
    function getJargon (xmlObject)

    // Get jargon terms
    {
        
    // Check that we actually have a valid response to process
        
    if (xmlObject.readyState == 4)
        {
            
    // Check for successful completion of HTTP session
            
    if (xmlObject.status == 200)
            {
                
    // Parse the returned XML
                
    xmlItems xmlObject.responseXML;
                if (
    xmlItems.getElementsByTagName('jargon')[0])
                {
                    
    xmlRoot xmlItems.documentElement;
                    
    // Get the jargon terms
                    
    if (xmlTerms xmlRoot.getElementsByTagName ('jargonterm'))
                    {
                        
    // Grab the content div for processing``
                        
    testData document.getElementById ('content');
                        
    // Set up Regular Expression
                        
    searchRegExp    = new RegExp ('''');
                        
    // Iterate over jargon term nodes
                        
    for (thisTerm 0thisTerm xmlTerms.lengththisTerm++)
                        {
                            if (
    thisDesc xmlTerms[thisTerm].getElementsByTagName ('description')[0])
                            
    // Create an entry for this item in the jargonTerms array
                            
    {
                                
    jargonTerms [xmlTerms[thisTerm].getAttribute ('id')] = '<p>This is the entry for ' xmlTerms[thisTerm].getAttribute ('id') + '</p>';
                            }
                            
    // Find the term in the content div and replace it with a link
                            
    searchRegExp.compile (xmlTerms[thisTerm].getAttribute ('term'), 'ig');
                            
    testData.innerHTML testData.innerHTML.replace (searchRegExp'<a class="jargonbuster" href="#" onclick="return (jargonDiv (\'' xmlTerms[thisTerm].getAttribute ('id') + '\'));">$&</a>');
                        }
                    }
                    else
                    {
                        
    // No jargon terms
                    
    }
                }
                else
                {
                    
    // Malformed response
                
    }
            }
            else
            {
                
    // Server error of some sort
            
    }
        }
        else
        {
            
    // Still waiting for operation to complete
        
    }

    The function that actually displays the data is as follows:

    PHP Code:
    function jargonDiv (selectedTerm)

    // Manage the jargon buster div
    {
        
    // If the div is already visable, then hide it
        
    try
        {
            
    document.body.removeChild (this);
        }
        catch (
    e) {}
        
    // Update div contents
        
    ajaxNode.innerHTML jargonTerms [selectedTerm];
        
    // Update div position
        
    ajaxNode.style.left    = (mouseX 100).toString () + 'px';
        
    ajaxNode.style.top    = (mouseY 2).toString () + 'px';
        
    // Show the div
        
    document.body.appendChild (ajaxNode);
        return (
    false);

    Does anyone know how to do this in a simple fashion?
    Last edited by PassiveSmoking; 04-03-2007 at 05:25 PM. Reason: Some code wasn't quite right

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Adirondacks
    Posts
    516
    Thanks
    4
    Thanked 4 Times in 4 Posts
    just pull info with an xsl stylesheet and skip that stuff

  • #3
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    Or you can do something like this.

    http://www.codingforums.com/showthread.php?t=109356
    Helping to build a bigger box. - Adam Matthews


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •