javascript - Is there a way to convert html to json?
高洛峰
高洛峰 2017-05-19 10:42:37
0
6
722

I recently received a job, which seems very ordinary. I use ajax to read the article and then load the article dynamically when the browser scrolls. But here comes the problem. The article source interface is provided in XML, and the article source and front-end are different. Not under the same domain name, this involves XML cross-domain issues, but the article source provides a crawling system that can use xslt, so that XML can be converted into html, then can html be converted into json and then accessed cross-domain? Or can xslt directly convert xml to json for cross-domain access?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(6)
Ty80
  1. Ask the other party to allow cross-domain access

  2. Write a service on your own backend to capture it, and then the frontend reads and displays it from your own server

左手右手慢动作

There is postHtml on the node side

If you don’t know the browser side, you can search it on github

滿天的星座

Constructor and export (can be changed to class)

function XmlToJson() {}
XmlToJson.prototype.setXml = function(xml) {
    if (xml && typeof xml == "string") {
        this.xml = document.createElement("p");
        this.xml.innerHTML = xml;
        this.xml = this.xml.getElementsByTagName("*")[0];
    } else if (typeof xml == "object") {
        this.xml = xml;
    }
};
XmlToJson.prototype.getXml = function() {
    return this.xml;
};
XmlToJson.prototype.parse = function(xml) {
    this.setXml(xml);
    return this.convert(this.xml);
};
XmlToJson.prototype.convert = function(xml) {
    if (xml.nodeType != 1) {
        return null;
    }
    var obj = {};
    obj.xtype = xml.nodeName.toLowerCase();
    var nodeValue = (xml.textContent || "").replace(/(\r|\n)/g, "").replace(/^\s+|\s+$/g, "");

    if (nodeValue && xml.childNodes.length == 1) {
        obj.text = nodeValue;
    }
    if (xml.attributes.length > 0) {
        for (var j = 0; j < xml.attributes.length; j++) {
            var attribute = xml.attributes.item(j);
            obj[attribute.nodeName] = attribute.nodeValue;
        }
    }
    if (xml.childNodes.length > 0) {
        var items = [];
        for (var i = 0; i < xml.childNodes.length; i++) {
            var node = xml.childNodes.item(i);
            var item = this.convert(node);
            if (item) {
                items.push(item);
            }
        }
        if (items.length > 0) {
            obj.items = items;
        }
    }
    return obj;
};
export { XmlToJson };

xml template string

let xml = `<viewport id="menuPane" layout="border">
    <panel region="center" border="0" layout="border">
        <tbar>
            <toolbar text="XXXX">
                <menu>
                    <text text="11">
                    </text>
                    <text text="22">
                    </text>
                    <text text="33">
                    </text>
                </menu>
            </toolbar>
            <toolbar text="XXXX">
                <menu>
                    <text text="44">
                    </text>
                    <text text="55">
                    </text>
                    <menu>
                        <text text="6 6">
                        </text>
                    </menu>
                    <text text="77">
                    </text>
                </menu>
            </toolbar>
        </tbar>
    </panel>
</viewport>`;

Call the method to convert xml to json and output it to the console for viewing

import { XmlToJson } from './xmlToJson.js';
let xmlParser = new XmlToJson();
let json = xmlParser.parse(xml);
console.log(JSON.stringify(json));//输出xml转换后的json
给我你的怀抱

Browser Ajax cross-domain has nothing to do with XML or JSON format.

What you need is an Ajax cross-domain solution.

黄舟

I don’t know if there is something wrong with my understanding. Aren’t the requirements of the questioner similar to those of a crawler? Use a crawler to crawl back and then parse it to the front desk? I don’t know if I understand it wrong, or it can also be achieved using a crawler

给我你的怀抱

You can use iframe to embed web pages in web pages

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!