需要帮助以Java脚本在html文件中显示XML内容

问题描述 投票:0回答:1

这是我的xml代码。我想用Javascript在html页面中显示内容。

<businesses>
    <business bfsId="" id="41481">
        <advertHeader>Welding Supplies, Equipment and Service Business</advertHeader>
        <Price>265000</Price>
        <catalogueDescription>Extremely profitable (Sales £500k, GP £182k) business</catalogueDescription>
        <keyfeature1>
        Well established 25 year business with excellent trading record
        </keyfeature1>
        <keyfeature2>
        Consistently high levels of turnover and profitability over last 5 years
        </keyfeature2>
    </business>
    <business bfsId="" id="42701">
        <broker bfsRef="1771" ref="003">Birmingham South, Wolverhampton &amp; West Midlands</broker>
        <tenure>freehold</tenure>
        <advertHeader>Prestigious Serviced Office Business</advertHeader>
        <Price>1200000</Price>
        <reasonForSale>This is a genuine retirement sale.</reasonForSale>
        <turnoverperiod>Annual</turnoverperiod>
        <established>28</established>
        <catalogueDescription>This well-located and long-established serviced office</catalogueDescription>
        <underOffer>No</underOffer>
        <image1>https://www.business-partnership.com/uploads/business/businessimg15977.jpg</image1>
        <keyfeature1>other connections</keyfeature1>
        <keyfeature2> Investment Opportunity</keyfeature2>
        <keyfeature3>Over 6,000 sq.ft.</keyfeature3>
        <keyfeature4>Well-maintained </keyfeature4>
        <keyfeature5>In-house services &amp; IT provided</keyfeature5>
    </business>
</businesses>

这是正在打印数据的html表

<table id="MainTable"><tbody id="BodyRows"></tbody></table>

而且我发现以下javascript代码可在html页面中显示xml内容。对于每个<business>元素,每行都会打印一次。对于<business>下的子元素,有一列。

window.addEventListener("load", function() {
            getRows();
        });

        function getRows() {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open("get", "2l.xml", true);
            xmlhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    showResult(this);
                }
            };
            xmlhttp.send(null);
        }

        function showResult(xmlhttp) {
            var xmlDoc = xmlhttp.responseXML.documentElement;
            removeWhitespace(xmlDoc);
            var outputResult = document.getElementById("BodyRows");
            var rowData = xmlDoc.getElementsByTagName("business");

            addTableRowsFromXmlDoc(rowData,outputResult);
        }

        function addTableRowsFromXmlDoc(xmlNodes,tableNode) {
            var theTable = tableNode.parentNode;
            var newRow, newCell, i;
            console.log ("Number of nodes: " + xmlNodes.length);
            for (i=0; i<xmlNodes.length; i++) {
                newRow = tableNode.insertRow(i);
                newRow.className = (i%2) ? "OddRow" : "EvenRow";
                for (j=0; j<xmlNodes[i].childNodes.length; j++) {
                    newCell = newRow.insertCell(newRow.cells.length);
                        //x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue); 
                        // var ah = getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue.getElementsByTagName("advertHeader")[0] 
                        //var advertHeader = xmlNodes[i].childNodes[j].getElementsByTagName();

                    if (xmlNodes[i].childNodes[j].firstChild) {
                        newCell.innerHTML = xmlNodes[i].childNodes[j].firstChild.nodeValue;
                    } else {
                        newCell.innerHTML = "-";
                    }
                    console.log("cell: " + newCell);

                }
                }
                theTable.appendChild(tableNode);
        }

        function removeWhitespace(xml) {
            var loopIndex;
            for (loopIndex = 0; loopIndex < xml.childNodes.length; loopIndex++)
            {
                var currentNode = xml.childNodes[loopIndex];
                if (currentNode.nodeType == 1)
                {
                    removeWhitespace(currentNode);
                }
                if (!(/\S/.test(currentNode.nodeValue)) && (currentNode.nodeType == 3))
                {
                    xml.removeChild(xml.childNodes[loopIndex--]);
                }
            }
        }

此JavaScript代码有效。但是,如您从xml代码中看到的那样,每个<business>元素下的子元素的数量都不同。这是原始xml文件https://alpha.business-sale.com/bfs.xml。一些<business>节点比其他节点具有更多的子节点。所以我得到的结果是这样的

enter image description here

第一行有5列,第二行有10列以上。

我想

  • 仅显示特定的子节点,例如<advertHeader><Price><catalogueDescription>,以便每一行显示相等的列数
  • 如果<Price>节点的值小于10000,我不想打印排

如何使用此代码执行此操作

javascript html xml dom xquery
1个回答
0
投票

此代码对我有用。这是完整的代码

© www.soinside.com 2019 - 2024. All rights reserved.