简体   繁体   中英

how to get table from a document in Node.js

I'm parsing an XML document to json using xml-js , one of the attribute is mark up language (HTML). Here is the XML after being parsed to JSON. here in "description":"_text": I get html.

  {
                    "name": {
                        "_text": "0"
                    },
                    "description": {
                        "_text": "<html xmlns:fo=\"http://www.w3.org/1999/XSL/Format\" xmlns:msxsl=\"urn:schemas-microsoft-com:xslt\">\n\n<head>\n\n<META http-equiv=\"Content-Type\" 
content=\"text/html\">\n\n<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">\n\n</head>\n\n<body style=\"margin:0px 0px 0px 0px;overflow:auto;background:#FFFFFF;\">\n\n<table style=\"font-family:Arial,Verdana,Times;font-size:12px;text-align:left;width:100%;border-collapse:collapse;padding:3px 3px 3px 3px\">\n\n<tr style=\"text-align:center;font-weight:bold;background:#9CBCE2\">\n\n<td>0</td>\n\n</tr>\n\n<tr>\n\n<td>\n\n<table style=\"font-family:Arial,Verdana,Times;font-size:12px;text-align:left;width:100%;border-spacing:0px; padding:3px 3px 3px 3px\">\n\n<tr>\n\n<td>FID</td>\n\n<td>0</td>\n\n</tr>\n\n<tr bgcolor=\"#D4E4F3\">\n\n<td>Id</td>\n\n<td>0</td>\n\n</tr>\n\n<tr>\n\n<td>City</td>\n\n<td>Islamabad</td>\n\n</tr>\n\n<tr bgcolor=\"#D4E4F3\">\n\n<td>Zones</td>\n\n<td>1</td>\n\n</tr>\n\n<tr>\n\n<td>Sector</td>\n\n<td>F-7</td>\n\n</tr>\n\n<tr bgcolor=\"#D4E4F3\">\n\n<td>Sub_Sector</td>\n\n<td>3</td>\n\n</tr>\n\n<tr>\n\n<td>Street_No</td>\n\n<td>0</td>\n\n</tr>\n\n<tr bgcolor=\"#D4E4F3\">\n\n<td>Gali_No</td>\n\n<td>0</td>\n\n</tr>\n\n<tr>\n\n<td>Plot_No</td>\n\n<td>0</td>\n\n</tr>\n\n<tr bgcolor=\"#D4E4F3\">\n\n<td>Area</td>\n\n<td>91849</td>\n\n</tr>\n\n<tr>\n\n<td>Land_Use</td>\n\n<td></td>\n\n</tr>\n\n<tr bgcolor=\"#D4E4F3\">\n\n<td>Road_Name</td>\n\n<td>Khayban-e-Iqbal</td>\n\n</tr>\n\n</table>\n\n</td>\n\n</tr>\n\n</table>\n\n</body>\n\n</html>\n\n"

                    }
                }

I have how can I parse the value of the table in description into json?

First cut the HTML table part of the string

const a = ... //the json object  
const b = a.description._text.indexOf('<table')
const c = a.description._text.indexOf('</table>')
const d = a.description._text.slice(b,c+8)
console.log(d)

Then use some html json converter such as https://www.npmjs.com/package/html2json

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