import * as fs from "fs-extra-plus"; import { JSDOM } from "jsdom"; import * as path from "path"; global.DOMParser = new JSDOM().window.DOMParser; let xmlParser = new DOMParser(); let str = fs.readFileSync(path.resolve(__dirname, "./OrderBom.xml"), "utf-8"); let xmlDoc = xmlParser.parseFromString(str, "text/xml"); let bomRoot = xmlDoc.children[0];//Bom bomRoot.tagName;//? let json = xml2json(bomRoot); for (let i = 0; i < bomRoot.children.length; i++) { let el = bomRoot.children.item(i); el.tagName;//? if (el.tagName === "Products")//模型列表 { for (let j = 0; j < el.children.length; j++) { let productEl = el.children.item(j);//模型(里面有板( 柜子?)) let categoryName = productEl.getAttribute("CategoryName");//? let name = productEl.getAttribute("Name");//? let roomId = productEl.getAttribute("RoomID"); //? let pid = productEl.getAttribute("PID"); //? let typeId = productEl.getAttribute("TypeID"); //? for (let i = 0; i < productEl.attributes.length; i++) { const att = productEl.attributes.item(i); att.name;//? att.value;//? } //CategoryName ChannelID Coordinate Coordinate_Parent Name OrderID PID Pos Pos_Parent RoomID Rot Rot_Parent TypeID if (typeId === "") { let paramTable = getElementsByDepth(productEl, "ParamTable"); let productss = getElementsByDepth(productEl, "Products"); for (let products of productss) for (let product of getElementsByDepth(products, "Product")) { console.log(product.tagName);//? let BoardMode = product.getAttribute("BoardMode"); if (BoardMode) { console.log(BoardMode); } for (let i = 0; i < product.attributes.length; i++) { const att = product.attributes.item(i); att.name;//? att.value;//? } } } else if (typeId === "XianTiao") { console.log(); for (let k = 0; k < productEl.children.length; k++) { let item = productEl.children.item(k); item.tagName;//? if (item.tagName === "") { } else if (item.tagName === "Product") { } } } } } } function xml2json(el: Element, obj = {}) { for (let i = 0; i < el.attributes.length; i++) { const att = el.attributes.item(i); obj[att.name] = att.value; } for (let i = 0; i < el.children.length; i++) { let cel = el.children.item(i); let tagName = cel.tagName; let arr = obj[tagName]; if (arr) arr.push(xml2json(cel)); else obj[tagName] = [xml2json(cel)]; } return obj; } function getElementsByDepth(el: Element, tagname: string, depth: number = 1, res: Element[] = []) { depth--; let children = el.children; for (let i = 0; i < children.length; i++) { if (children[i].tagName === tagname) { res.push(children[i]); if (depth > 0) getElementsByDepth(children[i], tagname, depth, res); } } return res; };