reactjs - React js,如何使用xml作为你的html设计
问题描述
我有一个从生成 xml 文件的 cms 工具创建的页眉/页脚。
我的代码:
import React, { useState, useEffect } from "react";
import axios from "axios";
export default function RegisterEntry() {
const [xml, setXML] = useState('');
axios.get('https://uhf.microsoft.com/en-US/shell/xml/MSIrelandsFuture?headerId=MSIrelandsFutureHeader&footerid=MSIrelandsFutureFooter', {
"Content-Type": "application/xml; charset=utf-8"
})
.then((response) => {
console.log('Your xml file as string', response.data);
setXML(response.data)
});
return (
<React.Fragment>
{xml}
</React.Fragment >
)
}
这些以某种方式返回了我的 xml 文本,我不确定如何返回实际的 ui 设计
我想要的结果:
解决方案
使用DOMParser将 XML 或 HTML 源代码从字符串解析为 DOM 文档。获取所需的节点并将其附加到文档的某些部分:
const appendToNode = (node, content) => {
node.innerHTML += content;
};
useEffect(() => {
const DOMParse = new DOMParser();
let xmlDoc;
axios
.get(
'https://uhf.microsoft.com/en-US/shell/xml/MSIrelandsFuture?headerId=MSIrelandsFutureHeader&footerid=MSIrelandsFutureFooter',
{
'Content-Type': 'application/xml; charset=utf-8'
}
)
.then(response => {
xmlDoc = DOMParse.parseFromString(response.data, 'text/xml');
appendToNode(
ref.current //--> some node in your document
xmlDoc.querySelector('headerHtml').textContent //--> do the same with the rest of the nodes
);
})
.catch(err => console.log(err));
}, []);
我建议您使用html-react-parser库将 HTML 字符串转换为 JSX 元素。
推荐阅读
- python - 如何在 Python 3 中构建 Windows 工具栏应用程序?
- javascript - JavaScript:如何解释和理解带有似乎没有价值的参数的函数?
- arrays - 如果用户输入将字符串留空,则默认为先前分配的奇怪行为 [已解决]
- java - 弹出菜单显示在不正确的位置
- javascript - 如何使用 JMuxer.js 播放 Java 的 FFmpegFrameGrabber Frame
- python-3.x - 如何在 python 中使用 *args 和类继承?
- r - 如何使用 R 中的分类变量获得回归的标准化 beta
- flutter - 地理围栏不在 Flutter 中调用事件
- java - 如何从java中的子项中获取json项
- tensorflow - AWS Notebook Instance is working but Lambda is not accepting the input