Home  >  Q&A  >  body text

javascript - Is there a way to convert html to json?

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?

高洛峰高洛峰2711 days ago701

reply all(6)I'll reply

  • PHP中文网

    PHP中文网2017-05-19 10:44:37

    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

    reply
    0
  • ringa_lee

    ringa_lee2017-05-19 10:44:37

    There is postHtml on the node side

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

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-19 10:44:37

    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

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-19 10:44:37

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

    What you need is an Ajax cross-domain solution.

    reply
    0
  • 黄舟

    黄舟2017-05-19 10:44:37

    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

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-19 10:44:37

    You can use iframe to embed web pages in web pages

    reply
    0
  • Cancelreply