简体   繁体   中英

Structuring display with indent JavaScript

I have input object that looks like this:

 myObj = {
        "Ob1": {
            "myObjName": "A1",
            "myObjType": "string",
            "myObjOcc": "minOccurs="1""
            "Ob2": {
                "myObjName": "B1",
                "myObjType": "string",
                "myObjOcc": "minOccurs="1""
                "Ob3": {
                    "myObjName": "C1",
                    "myObjType": "string",
                    "myObjOcc": "minOccurs="1""
                } 
            }
         }
         "Ob4": {
                    "myObjName": "A2",
                    "myObjType": "string",
                    "myObjOcc": "minOccurs="1""
         }
    }

And I have to display the objects in xml schema way:

<xs:complexType name="A1" type="string" minOccurs="1">
   <xs:complexType name="B1" type="string" minOccurs="1">
      <xs:simpleType name="C1" type="string" minOccurs="1"/>
   </complexType>
</complexType>
<xs:simpleType name="A2" type="string" minOccurs="1"/>

The idea is, if an object has child is a complexType if not, it's simpleType.

I have this code that is doing the printing, but if anyone can help me structure it with indent:

    function isNestedObject(obj) {
        for (var o in obj) {
            if (isComplexType(o, obj)) {
                return true;
            }
        }
    }

    function isComplexType(key, obj) {
        return (typeof obj[key] === "object");
    }

    function xsdStructure(obj) {
        var str = "",
            properties = obj.properties;              

        if (isNestedObject(obj)) {
            if (obj instanceof Array) {
                for(var o in obj) {
                    xsdStructure(obj[o]);
                }
            }
            str += "<xs:complexType name=\"" + obj.attrName + "\" type=\"" + obj.type + "\" " + obj.multiplicty + ">\n";
            for (var key in obj) {
                var arr = obj[key];
                if (arr instanceof Array) {
                    for (var a in arr) {
                        str += xsdStructure(arr[a]);
                    }
                }
            } 
            str += "</xs:complexType>\n"
        } else {
            str = "<xs:simpleType name=\"" + obj.attrName + "\" type=\"" + obj.type + "\" " + obj.multiplicty + "/>\n";
        }

        return str;
     }

     function printing(myObj) {
        var result = "";
        for (var key in object) {
            result += xsdStructure(object[key]);
        }
        result = '<xs:schema>\n' 
                        + result 
                    + '</xs:schema>';

        return result;
     }

It suppose to look like the above xml schema, but I can't use DOMParser so it has to be done manually.

Thanks in advance

I prepared fiddle for you. I had few things to mention for you. First, you should use the same attrs names among all you function which works with initial object. Ex: str += "<xs:complexType name=\\"" + obj.myObjName + "\\" type=\\"" + obj.myObjType + "\\" " + obj.myObjOcc + ">\\n"; and NOT your str += "<xs:complexType name=\\"" + obj.attrName + "\\" type=\\"" + obj.type + "\\" " + obj.multiplicty + ">\\n"; Also you will never get this condition true if (obj instanceof Array) because your obj doesn't contains arrays at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM