首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将xml显示为html?

如何将xml显示为html?
EN

Stack Overflow用户
提问于 2022-06-06 08:43:04
回答 2查看 324关注 0票数 0

我有包含html内容的XML文档。我如何将xml呈现为html的,我甚至尝试了react,它也失败了。

代码语言:javascript
复制
import React, { useEffect, useRef, useState } from 'react';
import { View } from 'react-native';
import { useWindowDimensions } from 'react-native';
import axios from 'axios';
import { DOMParser } from 'xmldom';
import RenderHtml from 'react-native-render-html';

export default function App() {
  const { width } = useWindowDimensions();
  const ref = useRef(null);
  const [xml, setXml] = useState(`
  <p style='text-align:center;'>
    Hello World!
  </p>`);

  const source = {
    html: xml,
  };

  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(
          document.head,
          xmlDoc.querySelector('cssIncludes').textContent,
        );
        appendToNode(
          document.head,
          xmlDoc.querySelector('javascriptIncludes').textContent,
        );
        appendToNode(
          ref.current,
          xmlDoc.querySelector('headerHtml').textContent,
        );
        appendToNode(
          ref.current,
          xmlDoc.querySelector('footerHtml').textContent,
        );
      })
      .catch(err => console.log(err));
  }, []);
  console.log('REFFFF', ref);
  return (
    <View style={{ flex: 1 }}>
      <View style={{ marginTop: 79 }} />
      <RenderHtml contentWidth={width} source={source} />
    </View>
  );
}

Think是使用react-native-render-html或任何第三方库将XML转换为html并在本机反应中呈现html。

谢谢预支:)

EN

回答 2

Stack Overflow用户

发布于 2022-06-06 10:17:35

Resive原住民文档目前推荐:

代码语言:javascript
复制
<WebView
    originWhitelist={['*']}
    source={{ html: '<p>Here I am</p>' }}
/>

https://github.com/react-native-webview/react-native-webview

如果不想嵌入WebView,也有第三方库将HTML呈现到本机视图中:

本机-渲染-html反应-本机-htmlview

票数 0
EN

Stack Overflow用户

发布于 2022-06-06 12:30:26

要将xml转换为html,您可以使用parseXML函数,在do显示之后,我们可以使用webview包“react本机-webview”显示如下所示的内容。

代码语言:javascript
复制
fetch('url')
  .then(data=>{
    data = $.parseXML(data);
    console.log(data);
  })
  .catch((err)=>{
    console.log(err);
  });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72515123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档