【问题标题】:how to get xml document out of html document如何从html文档中获取xml文档
【发布时间】:2019-12-28 21:59:20
【问题描述】:

我在使用 Chrome 时无法在 Chrome 中检索完整的 xml 文档 xmlDOM = document.implementation.createDocument("", "", null); 初始创建文档。 创建 DOM 后,我会动态地将元素添加到其中

function setUsersSearchVariables(_firstName,_lastName,_Username,_Email,_Phone,_US_Emp_Pub,_US_Both,_CS_InAct_Act,_CS_Both, _companyName, _accountType, _tAll)
{
    try
    {
        var dataDoc = new DataDocument();

        dataDoc.mode = 'SET_SEARCH_USER_SESSION_VARIABLES';
        dataDoc.appendKeyElement('COMPANY_NAME', _companyName);
        dataDoc.appendKeyElement('FIRST_NAME',_firstName);
        dataDoc.appendKeyElement('LAST_NAME',_lastName);
        dataDoc.appendKeyElement('USER_NAME',_Username);
        dataDoc.appendKeyElement('EMAIL',_Email);
        dataDoc.appendKeyElement('PHONE',_Phone);
        dataDoc.appendKeyElement('US_EMP_PUB',_US_Emp_Pub);
        dataDoc.appendKeyElement('US_BOTH',_US_Both);
        dataDoc.appendKeyElement('CS_INACT_ACT',_CS_InAct_Act);
        dataDoc.appendKeyElement('CS_BOTH', _CS_Both);
        dataDoc.appendKeyElement('ACCOUNT_TYPE', _accountType);
        dataDoc.appendKeyElement('TYPE_BOTH', _tAll);
        dataDoc.sendToServer();
        return false;
    }
    catch(e)
    {
        alert('Block: setUsersSearchVariables\n'+e.message);
        return false;
    }
}
  var rootNodeName = 'POST_DATA';
    var modeAttrName = 'MODE';

var appendKeyElementRef = function (key, value) {
    try {
        this.validateDoc();

        var keyNode = this.doc.createElement(key);

        if (value.toString() != '') {
            var cdataNode = this.doc.createCDATASection(value.toString());
            keyNode.appendChild(cdataNode);
        }

        var rootNode = this.doc.getElementsByTagName(rootNodeName)[0];
        //var rootNode = this.getElementsByTagName(rootNodeName)[0];
        rootNode.appendChild(keyNode);
    }
    catch (ex) {
        alert('Block: appendKeyElementRef\n' + ex.message);
    }
};

数据文件:

function DataDocument()
{
    // Member properties
    this.mode = '';
    this.gcNames = null;
    this.target = null;
    this.callbackControlId = null;
    this.callbackId = null;
    this.section = null;
    this.sessionOutTime = 0;
    this.sessionOutWarningTime = 0;
    this.browserType = getBrowserType();
    this.doc = createDoc(this.browserType);
}

// Member functions of DataDocument class

// Type : Public
// To load string data into DataDocument 
DataDocument.prototype.load = loadRef;

// Type : Private
// To check DataDocument for root node
// If root node does not exist, it will be created
DataDocument.prototype.validateDoc = validateDocRef;

// Type : Public
// To append a key-value pair with the DataDocument
DataDocument.prototype.appendKeyElement = appendKeyElementRef;

// Type : Public
// To append a control as an element node with the DataDocument
DataDocument.prototype.appendControlElement = appendControlElementRef;

// Type : Public
// To append a container's child controls as element nodes with the DataDocument
DataDocument.prototype.appendContainerElement = appendContainerElementRef;

// Type : Private
// To append an element node with the DataDocument
DataDocument.prototype.appendElementNode = appendElementNodeRef;

// Type : Private
// To append the MODE attribure for the DataDocument root node
DataDocument.prototype.appendModeAttribute = appendModeAttributeRef;

// Type : Private
// To append the SENDER attribure for the DataDocument root node
DataDocument.prototype.appendSenderAttribute = appendSenderAttributeRef;

// Type : Private
// To append the SECTION attribure for the DataDocument root node
DataDocument.prototype.appendSectionAttribute = appendSectionAttributeRef;

// Type : Public
// To get the value of element node by element id
DataDocument.prototype.getValueByElementId = getValueByElementIdRef;

// Type : Public
// To get the value of key-value node by key name
DataDocument.prototype.getValueByKeyName = getValueByKeyNameRef;

// Type : Public
// To get the DataDocument content as string
DataDocument.prototype.toString = toStringRef;

// Type : Private
// To get the DataDocument content as string to send for callback
// It will add the MODE and SENDER attributes before generate the string
DataDocument.prototype.toStringForCallback = toStringForCallbackRef;

// Type : Public
// Raise the callback from client side with the DataDocument as content
DataDocument.prototype.sendToServer = sendToServerRef;

DataDocument.prototype.getElementsByTagName = getElementsByTagNameRef;
DataDocument.prototype.getElementsByTagNameInternal = getElementsByTagNameInternalRef;

我读到document.implementation.createDocument("", "", null); 创建了一个 html 文档。然后我尝试使用this.doc.documentElement.innerHTML; 获取 XML。这给了我一个没有正确根节点和模式的 Html 字符串。

我们在chrome中的innerHtml

"<COMPANY_NAME/><FIRST_NAME><![CDATA[Name]]></FIRST_NAME><LAST_NAME/><USER_NAME/><EMAIL/><PHONE><![CDATA[(___)___-____]]></PHONE><US_EMP_PUB/><US_BOTH><![CDATA[True]]></US_BOTH><CS_INACT_ACT/><CS_BOTH><![CDATA[True]]></CS_BOTH><ACCOUNT_TYPE/><TYPE_BOTH><![CDATA[True]]></TYPE_BOTH>"

我需要的 XML 应该是这样的:

<POST_DATA SENDER="CLIENT" MODE="SET_SEARCH_USER_SESSION_VARIABLES"><COMPANY_NAME/><FIRST_NAME><![CDATA[chris]]></FIRST_NAME><LAST_NAME/><USER_NAME/><EMAIL/><PHONE><![CDATA[(___)___-____]]></PHONE><US_EMP_PUB/><US_BOTH><![CDATA[True]]></US_BOTH><CS_INACT_ACT/><CS_BOTH><![CDATA[True]]></CS_BOTH><ACCOUNT_TYPE/><TYPE_BOTH><![CDATA[True]]></TYPE_BOTH></POST_DATA>

因此,如果我将 IE 与 ActiveXObject 一起使用,则此方法有效,但是在使用 Chrome 时它不起作用。我已经阅读了所有我能找到的关于 HTML DOM 和 XML DOM 的内容,我只是想不通。希望你们能提供帮助。

【问题讨论】:

    标签: javascript html xml dom


    【解决方案1】:

    您发布了不完整的代码,实际上并没有在任何地方创建根 &lt;POST_DATA/&gt; 元素。

    正确的调用类似于(参见createDocument documentation):

    var xmlDoc = document.implementation.createDocument ('', 'POST_DATA', null);
    

    要序列化 ​​XML,您不应该使用 innerHTML:how to convert xml document object to string?

    var s = new XMLSerializer();
    var docStr = s.serializeToString(xmlDoc); // returns "<POST_DATA/>" for the xmlDoc above
    

    【讨论】:

    • 谢谢,我今天就试试这个。
    【解决方案2】:

    感谢 Nick,我找到了我已经拥有的解决方案。我只需要序列化文档。

     return (new XMLSerializer()).serializeToString(thisDoc);
    

    我现在可能还有一两个星期的工作哇!!再次感谢尼克。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-18
      • 2020-03-18
      • 1970-01-01
      • 2013-12-10
      • 1970-01-01
      • 2015-07-09
      • 2014-03-31
      相关资源
      最近更新 更多