Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
V
vue3_onlineEditor
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
pangchong
vue3_onlineEditor
Commits
0cdd9946
Commit
0cdd9946
authored
Mar 28, 2025
by
qlintonger xeno
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: AI格式化processing
parent
878ee200
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
73 additions
and
38 deletions
+73
-38
src/lib/XMLProcessor/src/core/Processing.ts
+73
-38
No files found.
src/lib/XMLProcessor/src/core/Processing.ts
View file @
0cdd9946
import
{
TreeRenderResult
}
from
'@/lib/XMLProcessor/src/typing'
// 引入 TreeRenderResult 类型,该类型定义在 @/lib/XMLProcessor/src/typing 模块中
import
{
UUID
}
from
'uuidjs'
import
{
TreeRenderResult
}
from
'@/lib/XMLProcessor/src/typing'
;
// 引入 UUID 类用于生成唯一标识符
import
{
UUID
}
from
'uuidjs'
;
// 定义 Processing 类,用于处理 XML 数据
export
class
Processing
{
export
class
Processing
{
private
domParser
:
DOMParser
// 私有属性,用于解析 XML 字符串为 DOM 对象
private
domParser
:
DOMParser
;
// 构造函数,初始化 DOMParser 实例
constructor
()
{
constructor
()
{
this
.
domParser
=
new
DOMParser
()
this
.
domParser
=
new
DOMParser
()
;
}
}
processXML
(
xmlContent
:
string
,
handledNode
:
string
[]):{
// 处理 XML 字符串的方法,返回树形数据和 XML DOM 对象
treeData
:
TreeRenderResult
[],
xmlDOM
:
Document
processXML
(
xmlContent
:
string
,
handledNode
:
string
[]):
{
treeData
:
TreeRenderResult
[];
xmlDOM
:
Document
;
}
{
}
{
const
parsedTree
=
this
.
domParser
.
parseFromString
(
xmlContent
,
'text/xml'
)
// 使用 DOMParser 解析 XML 字符串
let
rootElement
=
parsedTree
.
documentElement
const
parsedTree
=
this
.
domParser
.
parseFromString
(
xmlContent
,
'text/xml'
);
// 获取 XML 文档的根元素
const
rootElement
=
parsedTree
.
documentElement
;
return
{
return
{
// 调用 innerHandle 方法处理根元素,生成树形数据
treeData
:
this
.
innerHandle
(
rootElement
,
handledNode
),
treeData
:
this
.
innerHandle
(
rootElement
,
handledNode
),
// 返回解析后的 XML DOM 对象
xmlDOM
:
parsedTree
xmlDOM
:
parsedTree
}
}
;
}
}
async
processFile
(
xmlFile
:
File
,
handledNode
:
string
[]):
Promise
<
{
treeData
:
TreeRenderResult
[],
xmlDOM
:
Document
}
>
{
// 异步处理 XML 文件的方法,返回包含树形数据和 XML DOM 对象的 Promise
async
processFile
(
xmlFile
:
File
,
handledNode
:
string
[]):
Promise
<
{
treeData
:
TreeRenderResult
[];
xmlDOM
:
Document
}
>
{
// 检查文件是否为有效的 XML 文件
if
(
!
(
xmlFile
instanceof
File
)
||
!
xmlFile
.
type
.
includes
(
'xml'
))
{
if
(
!
(
xmlFile
instanceof
File
)
||
!
xmlFile
.
type
.
includes
(
'xml'
))
{
return
Promise
.
reject
(
'The file is not a valid XML file.'
)
return
Promise
.
reject
(
'The file is not a valid XML file.'
)
;
}
}
return
new
Promise
((
resolve
,
reject
)
=>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
const
fr
=
new
FileReader
();
const
fr
=
new
FileReader
();
// 当文件读取完成时触发的事件处理函数
fr
.
onload
=
()
=>
{
fr
.
onload
=
()
=>
{
const
content
=
fr
.
result
as
string
;
const
content
=
fr
.
result
as
string
;
try
{
try
{
const
{
treeData
,
xmlDOM
}
=
this
.
processXML
(
content
,
handledNode
)
// 调用 processXML 方法处理读取的文件内容
resolve
({
treeData
,
xmlDOM
})
const
{
treeData
,
xmlDOM
}
=
this
.
processXML
(
content
,
handledNode
);
// 解析成功,返回树形数据和 XML DOM 对象
resolve
({
treeData
,
xmlDOM
});
}
catch
(
error
)
{
}
catch
(
error
)
{
reject
(
error
)
// 解析失败,拒绝 Promise 并返回错误信息
}
reject
(
error
);
}
}
fr
.
onerror
=
function
()
{
};
reject
(
'Error reading file.'
)
// 当文件读取出错时触发的事件处理函数
}
fr
.
onerror
=
()
=>
{
reject
(
'Error reading file.'
);
};
// 以文本形式读取文件
fr
.
readAsText
(
xmlFile
);
fr
.
readAsText
(
xmlFile
);
})
})
;
}
}
// 私有方法,用于递归处理单个 DOM 节点,生成树形数据
private
innerHandle
(
domNode
:
Element
,
handledNode
:
string
[]):
TreeRenderResult
[]
{
private
innerHandle
(
domNode
:
Element
,
handledNode
:
string
[]):
TreeRenderResult
[]
{
let
treeData
:
TreeRenderResult
[]
=
[];
let
treeData
:
TreeRenderResult
[]
=
[];
// 必须添加根节点内容
// 生成唯一的 key
let
targetKey
=
UUID
.
generate
()
const
targetKey
=
UUID
.
generate
();
let
treeItem
:
TreeRenderResult
=
{
// 创建树形数据项
const
treeItem
:
TreeRenderResult
=
{
key
:
targetKey
,
key
:
targetKey
,
label
:
domNode
.
nodeName
,
label
:
domNode
.
nodeName
,
children
:
[],
children
:
[],
index
:
0
,
index
:
0
,
chained
:
[
0
]
chained
:
[
0
]
}
};
// 为 DOM 节点设置 data-key 属性
domNode
.
setAttribute
(
'data-key'
,
targetKey
);
domNode
.
setAttribute
(
'data-key'
,
targetKey
);
treeItem
.
children
=
this
.
innerGroupHandle
(
domNode
.
children
,
handledNode
,
[
0
])
// 递归处理子节点
treeData
.
push
(
treeItem
)
treeItem
.
children
=
this
.
innerGroupHandle
(
domNode
.
children
,
handledNode
,
[
0
]);
// 将树形数据项添加到结果数组中
treeData
.
push
(
treeItem
);
return
treeData
return
treeData
;
}
}
private
innerGroupHandle
(
nodeSet
:
HTMLCollection
,
handledNode
:
string
[],
startChained
:
number
[])
{
// 私有方法,用于递归处理一组 DOM 节点,生成树形数据
let
targetResult
:
TreeRenderResult
[]
=
[]
private
innerGroupHandle
(
nodeSet
:
HTMLCollection
,
handledNode
:
string
[],
startChained
:
number
[]):
TreeRenderResult
[]
{
let
targetResult
:
TreeRenderResult
[]
=
[];
let
realIndex
=
-
1
;
let
realIndex
=
-
1
;
// 遍历节点集合
for
(
let
i
=
0
;
i
<
nodeSet
.
length
;
i
++
)
{
for
(
let
i
=
0
;
i
<
nodeSet
.
length
;
i
++
)
{
const
node
=
nodeSet
[
i
]
as
Element
;
const
node
=
nodeSet
[
i
]
as
Element
;
// 检查节点是否在处理列表中
if
(
!
handledNode
.
includes
(
node
.
nodeName
))
{
if
(
!
handledNode
.
includes
(
node
.
nodeName
))
{
// 如果节点有子节点,递归处理子节点
if
(
node
.
children
.
length
)
{
if
(
node
.
children
.
length
)
{
targetResult
.
push
(...
this
.
innerGroupHandle
(
node
.
children
,
handledNode
,
startChained
))
targetResult
.
push
(...
this
.
innerGroupHandle
(
node
.
children
,
handledNode
,
startChained
));
}
}
continue
continue
;
}
}
realIndex
++
;
realIndex
++
;
let
targetKey
=
UUID
.
generate
()
// 生成唯一的 key
let
treeItem
:
TreeRenderResult
=
{
const
targetKey
=
UUID
.
generate
();
// 创建树形数据项
const
treeItem
:
TreeRenderResult
=
{
key
:
targetKey
,
key
:
targetKey
,
label
:
node
.
nodeName
,
label
:
node
.
nodeName
,
index
:
realIndex
,
index
:
realIndex
,
chained
:
[...
startChained
,
realIndex
]
chained
:
[...
startChained
,
realIndex
]
}
};
// 如果节点名为 CEP,打印树形数据项
if
(
treeItem
.
label
===
'CEP'
)
{
if
(
treeItem
.
label
===
'CEP'
)
{
console
.
log
(
'the res here'
,
treeItem
)
console
.
log
(
'the res here'
,
treeItem
)
;
}
}
// 为 DOM 节点设置 data-key 属性
node
.
setAttribute
(
'data-key'
,
targetKey
);
node
.
setAttribute
(
'data-key'
,
targetKey
);
// 如果节点有子节点,递归处理子节点
if
(
node
.
children
.
length
>
0
)
{
if
(
node
.
children
.
length
>
0
)
{
treeItem
.
children
=
this
.
innerGroupHandle
(
node
.
children
,
handledNode
,
[...
startChained
,
realIndex
])
treeItem
.
children
=
this
.
innerGroupHandle
(
node
.
children
,
handledNode
,
[...
startChained
,
realIndex
])
;
}
}
targetResult
.
push
(
treeItem
)
// 将树形数据项添加到结果数组中
targetResult
.
push
(
treeItem
);
}
}
return
targetResult
return
targetResult
;
}
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment