mirror of
https://gitlab.com/bramw/baserow.git
synced 2025-05-19 15:11:29 +00:00
📄4️⃣ Async file import v4: add progress bar while importing data
This commit is contained in:
parent
9d45fd2396
commit
bdf183facb
60 changed files with 2047 additions and 1164 deletions
web-frontend/modules/database/utils
|
@ -1,34 +1,61 @@
|
|||
export class XMLParser {
|
||||
constructor() {
|
||||
this.header = []
|
||||
this.errors = []
|
||||
this.xmlDoc = null
|
||||
this.xmlData = null
|
||||
}
|
||||
|
||||
parse(rawXML) {
|
||||
this.xmlDoc = new window.DOMParser().parseFromString(rawXML, 'text/xml')
|
||||
const parseErrors = this.xmlDoc.getElementsByTagName('parsererror')
|
||||
if (parseErrors.length > 0) {
|
||||
Array.from(parseErrors).forEach((parseError) =>
|
||||
this.errors.push(parseError.textContent)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
loadXML(count) {
|
||||
if (
|
||||
this.xmlDoc &&
|
||||
this.xmlDoc.documentElement &&
|
||||
this.xmlDoc.documentElement.children
|
||||
) {
|
||||
let dataToLoad = Array.from(this.xmlDoc.documentElement.children)
|
||||
if (count) {
|
||||
dataToLoad = dataToLoad.slice(0, count)
|
||||
}
|
||||
this.xmlData = dataToLoad.map((row) => {
|
||||
const values = Array.from(row.children).map((rowChild) => {
|
||||
const rowTag = rowChild.tagName
|
||||
if (!this.header.includes(rowTag)) {
|
||||
this.header.push(rowTag)
|
||||
}
|
||||
return { tag: rowTag, value: rowChild.innerHTML }
|
||||
})
|
||||
return values
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
transform() {
|
||||
this.xmlData = this.xmlData.map((line) => {
|
||||
return this.header.map((h) => {
|
||||
const lineValue = line.filter((lv) => lv.tag === h)
|
||||
return lineValue.length > 0 ? lineValue[0].value : ''
|
||||
})
|
||||
})
|
||||
return [this.header, this.xmlData, this.errors]
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a rawXML string and extracts tabular data from it.
|
||||
*/
|
||||
export const parseXML = (rawXML) => {
|
||||
let xmlData = []
|
||||
const header = []
|
||||
const xmlDoc = new window.DOMParser().parseFromString(rawXML, 'text/xml')
|
||||
const parseErrors = xmlDoc.getElementsByTagName('parsererror')
|
||||
const errors = []
|
||||
if (parseErrors.length > 0) {
|
||||
Array.from(parseErrors).forEach((parseError) =>
|
||||
errors.push(parseError.textContent)
|
||||
)
|
||||
}
|
||||
if (xmlDoc && xmlDoc.documentElement && xmlDoc.documentElement.children) {
|
||||
xmlData = Array.from(xmlDoc.documentElement.children).map((row) => {
|
||||
const vals = Array.from(row.children).map((rowChild) => {
|
||||
const rowTag = rowChild.tagName
|
||||
if (!header.includes(rowTag)) {
|
||||
header.push(rowTag)
|
||||
}
|
||||
return { tag: rowTag, value: rowChild.innerHTML }
|
||||
})
|
||||
return vals
|
||||
})
|
||||
}
|
||||
xmlData = xmlData.map((line) => {
|
||||
return header.map((h) => {
|
||||
const lineValue = line.filter((lv) => lv.tag === h)
|
||||
return lineValue.length > 0 ? lineValue[0].value : ''
|
||||
})
|
||||
})
|
||||
return [header, xmlData, errors]
|
||||
const parser = new XMLParser()
|
||||
parser.parse(rawXML)
|
||||
parser.loadXML()
|
||||
return parser.transform()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue