[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.2

JavaScript to parse an HTML document and create simple Document Object Model (DOM)

Asked by omicronpersei8 in JavaScript, Macromedia Homesite, Dynamic HTML (DHTML)

Tags: javascript

I need a functions to create a DOM from a HTML document. I am experience in JavaScript but I cannot figure this out. Here is a skeleton of functions DreamweaverMX uses to create XML documents. Macromedia gives extension developers permission to use their functions and methods but I do think this will work with HTML files. Here is the functions and beneath them there are more details listing exactly what I need.

XMLDoc:
function XMLDoc(source, errFn) {
    this.topNode = null;
    this.errFn = errFn;
    this.createXMLNode = _XMLDoc_createXMLNode;
    this.error = _XMLDoc_error;
    this.getUnderlyingXMLText = _XMLDoc_getUnderlyingXMLText;
    this.handleNode = _XMLDoc_handleNode;
    this.hasErrors = false;
    this.insertNodeAfter = _XMLDoc_insertNodeAfter;
    this.insertNodeInto = _XMLDoc_insertNodeInto;
    this.parse = _XMLDoc_parse;
    this.parseAttribute = _XMLDoc_parseAttribute;
    this.parseDTD = _XMLDoc_parseDTD;
    this.parsePI = _XMLDoc_parsePI;
    this.parseTag = _XMLDoc_parseTag;
    this.removeNodeFromTree = _XMLDoc_removeNodeFromTree;
    this.replaceNodeContents = _XMLDoc_replaceNodeContents;
    this.source = source;
    if (this.parse()) {
        if (this.topNode != null) {
            return this.error("expected close " + this.topNode.tagName);
        } else {
            return true;
        }
    }
    return null;
}

_XMLDoc_parse:
function _XMLDoc_parse() {
    var pos = 0;
    err = false;
    while (!err) {
        var closing_tag_prefix = "";
        var chpos = this.source.indexOf("<", pos);
        var open_length = 1;
        var open;
        var close;
        if (chpos == -1) {
            break;
        }
        open = chpos;
        var str = this.source.substring(pos, open);
        if (str.length != 0) {
            err = !this.handleNode(new XMLNode("TEXT", this, str));
        }
        if (chpos == this.source.indexOf("<?", pos)) {
            pos = this.parsePI(this.source, pos + 2);
            if (pos == 0) {
                err = true;
            }
            continue;
        }
        if (chpos == this.source.indexOf("<!DOCTYPE", pos)) {
            pos = this.parseDTD(this.source, chpos + 9);
            if (pos == 0) {
                err = true;
            }
            continue;
        }
        if (chpos == this.source.indexOf("<!--", pos)) {
            open_length = 4;
            closing_tag_prefix = "--";
        }
        if (chpos == this.source.indexOf("<![CDATA[", pos)) {
            open_length = 9;
            closing_tag_prefix = "]]";
        }
        chpos = this.source.indexOf(closing_tag_prefix + ">", chpos);
        if (chpos == -1) {
            return this.error("expected closing tag sequence: " + closing_tag_prefix + ">");
        }
        close = chpos + closing_tag_prefix.length;
        str = this.source.substring(open + 1, close);
        var n = this.parseTag(str);
        if (n) {
            err = !this.handleNode(n);
        }
        pos = close + 1;
    }
    return !err;
}
_XMLDoc_handleNode:
function _XMLDoc_handleNode(current) {
    if ((current.nodeType == "COMMENT") && (this.topNode != null)) {
        return this.topNode.addElement(current);
    } else {
        if ((current.nodeType == "TEXT") || (current.nodeType == "CDATA")) {
            if (this.topNode == null) {
                if (trim(current.content, true, false) == "") {
                    return true;
                } else {
                    return this.error("expected document node, found: " + current);
                }
            } else {
                return this.topNode.addElement(current);
            }
        } else {
            if ((current.nodeType == "OPEN") || (current.nodeType == "SINGLE")) {
                var success = false;
                if (this.topNode == null) {
                    this.docNode = current;
                    current.parent = null;
                    success = true;
                } else {
                    success = this.topNode.addElement(current);
                }
                if (success && (current.nodeType != "SINGLE")) {
                    this.topNode = current;
                }
                current.nodeType = "ELEMENT";
                return success;
            } else {
                if (current.nodeType == "CLOSE") {
                    if (this.topNode == null) {
                        return this.error("close tag without open: " + current.toString());
                    } else {
                        if (current.tagName != this.topNode.tagName) {
                            return this.error("expected closing " + this.topNode.tagName + ", found closing " + current.tagName);
                        } else {
                            this.topNode = this.topNode.getParent();
                        }
                    }
                }
            }
        }
    }
    return true;
}

I have only included three main functions.  The above is only used for a reference. I don't think it will work by it's self nor do I need all the extra functions it seems to have. Only the functions listed below.

I need to be able to do this:

var theDOM = new HTMLDOM(strHTML); // this builds a html document object model from a html document in a string.
var theNode = theDOM.getElementByID("sna"); // This gets the node by it's ID. eg, "<td id='sna'>some text</td>" returns a node of this td tag;
var theNode = theDOM.getNodeByIDorName("NameOrID");
var theParent = theNode.parentNode; // if tag was in
var intImgWidth = theNode.getAttribute('width');
theNode.setAttribute("align",curAlign); // sets tag attribute
theNode.removeAttribute("align",curAlign); // removes tag attribute
var tagName = theNode.tagName; // gets tagName. eg "<img src='fds.gif'>" returns "img"
var innerHTML = theNode.innerHTML(); // returns everything inside a tag. eg "<div>some text</div>" returns "some text"
var outerHTML = theNode.outerHTML(); // the same as above but with the tags included
theNode.innerHTML = "<div style='width:135'>some text</div>"; // this would set the innerHTML property
var sourceHTML = theDOM.source; // returns the whole document in the form of a string.

I dont know if this is a lot of work for an actual educated programmer. I am self-educated. If you build this from scratch and then decide to sell it then go for it. All I want is a licensed copy to include my project I dont care what you do with it. I can send you all the functions that go with this script in a separate email. You can also enumerate them in the DMX enviornment. You can do this by typing dw.getDocumentDOM().parentWindow in my script console extension I've built. Ask for it and I will send it to you. It is requires Dreamweaver MX.

Omicron Persei 8
[+][-]09/02/03 09:53 PM, ID: 9271974Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/02/03 10:56 PM, ID: 9272173Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/02/03 11:40 PM, ID: 9272339Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/03/03 07:34 PM, ID: 9284656Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/04/03 02:33 AM, ID: 9286403Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]09/04/03 11:46 AM, ID: 9290622Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/04/03 06:47 PM, ID: 9292817Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]09/04/03 08:22 PM, ID: 9293153Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/05/03 05:18 AM, ID: 9295211Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/05/03 10:38 AM, ID: 9297540Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/06/03 07:32 AM, ID: 9301404Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: JavaScript, Macromedia Homesite, Dynamic HTML (DHTML)
Tags: javascript
Sign Up Now!
Solution Provided By: Nushi
Participating Experts: 3
Solution Grade: B
 
[+][-]09/09/03 10:48 AM, ID: 9322586Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/15/03 03:04 AM, ID: 9361591Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-89