reactjs - 如何在 React 对话框中查看格式化的 xml

标签 reactjs

我学习 React 并希望在这样的对话框中显示 xml:

import React from 'react';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import TypoGraphy from '@material-ui/core/Typography'
import Button from '@material-ui/core/Button';
import { makeStyles } from '@material-ui/core/styles';
import Dialog from '@material-ui/core/Dialog';
import DialogActions from '@material-ui/core/DialogActions';
import DialogContent from '@material-ui/core/DialogContent';
import DialogContentText from '@material-ui/core/DialogContentText';
import DialogTitle from '@material-ui/core/DialogTitle';


const useStyles = makeStyles(theme => ({
    root: {
        flexGrow: 1,
    },
    menuButton: {
        marginRight: theme.spacing(2),
    },
    title: {
        flexGrow: 1,
    },
}));


const NavBar = () => {
    const [open, setOpen] = React.useState(false);
    const [scroll, setScroll] = React.useState('paper');

    const handleClickOpen = scrollType => () => {
        setOpen(true);
        setScroll(scrollType);
    };

    function handleClose() {
        setOpen(false);
    }
    const classes = useStyles();
    return (
        <div>
            <AppBar color="primary" position="static">
                <Toolbar>
                    <TypoGraphy variant="title" color="inherit" className={classes.title} >
                        React Redux App Book Search Api
                   </TypoGraphy>
                    <Button onClick={handleClickOpen('paper')} color="inherit">View DB </Button>
                    <Dialog
                        open={open}
                        onClose={handleClose}
                        scroll={scroll}
                        aria-labelledby="scroll-dialog-title"
                    >
                        <DialogTitle id="scroll-dialog-title">Subscribe</DialogTitle>
                        <DialogContent dividers={scroll === 'paper'}>
                            <DialogContentText>
                                {[...new Array(50)]
                                    .map(
                                        () => `<?xml version="1.0"?>
<catalog>
  <book id="B1">
    <author>Kutner, Joe</author>
    <title>Deploying with JRuby</title>
    <genre>Computer</genre>
    <price>33.00</price>
    <publish_date>2012-08-15</publish_date>
    <description>Deploying with JRuby is the missing link between enjoying JRuby and using it in the real world to build high-performance, scalable applications.</description>
  </book>
  <book id="B2">
    <author>Ralls, Kim</author>
    <title>Midnight Rain</title>
    <genre>Fantasy</genre>
    <price>5.95</price>
    <publish_date>2000-12-16</publish_date>
    <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>
  </book>
  <book id="B3">
    <author>Corets, Eva</author>
    <title>Maeve Ascendant</title>
    <genre>Fantasy</genre>
    <price>5.95</price>
    <publish_date>2000-11-17</publish_date>
    <description>After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.</description>
  </book>
</catalog>`,
                                    )
                                    .join('\n')}
                            </DialogContentText>
                        </DialogContent>
                        <DialogActions>
                            <Button onClick={handleClose} color="primary">
                                Cancel
          </Button>
                            <Button onClick={handleClose} color="primary">
                                Subscribe
          </Button>
                        </DialogActions>
                    </Dialog>
                </Toolbar>
            </AppBar>
        </div>
    )
}
export default NavBar;

但是格式不正确,无法读取。有没有办法很好地格式化它?

enter image description here

更新感谢@DanAndreasson

我已经尝试过:(使用 https://github.com/svenheden/xml-beautifier )

import beautify from 'xml-beautifier';


const xml = beautify(`<?xml version="1.0"?>
<catalog>
  <book id="B1">
    <author>Kutner, Joe</author>
    <title>Deploying with JRuby</title>
    <genre>Computer</genre>
    <price>33.00</price>
    <publish_date>2012-08-15</publish_date>
    <description>Deploying with JRuby is the missing link between enjoying JRuby and using it in the real world to build high-performance, scalable applications.</description>
  </book>
</catalog>`)

然后在渲染中:

   <DialogContent dividers={scroll === 'paper'}>
       <DialogContentText>
             <div>{xml}</div>
       </DialogContentText>
   </DialogContent>

但是这个“xml-beautifier”什么也不做,我想我需要在 DialogContentText 中添加更多内容才能使其正常工作

最佳答案

对于其他对此不熟悉的人,我使用以下方法使其工作:

https://www.npmjs.com/package/xml-beautifier

基本上是这样的:

var xmlText = `<? xml version = "1.0" ?>
    <catalog>
        <book id="B1">
            <author>Kutner, Joe</author>
            <title>Deploying with JRuby</title>
            <genre>Computer</genre>
            <price>33.00</price>
            <publish_date>2012-08-15</publish_date>
            <description>Deploying with JRuby is the missing link between enjoying JRuby and using it in the real world to build high-performance, scalable applications.</description>
        </book>
    </catalog>`;

然后使用美化:

const result1 = beautify(xmlText);

并在 Material-ui 中像这样渲染它:

<DialogContentText>
     {result1}
</DialogContentText>

关于reactjs - 如何在 React 对话框中查看格式化的 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57500534/

相关文章:

reactjs - 类型 'Element' 缺少类型 'SVGSVGElement' 的以下属性

javascript - 如何让 visual studio 代码不把我的双引号变成单引号?

html - 使用html中的react组件作为html标签

javascript - 从类组件切换到函数组件

javascript - 检查 Jest 中功能组件的状态值

javascript - 你应该在 React.useEffect() 中调用 navigate(),而不是在你的组件第一次渲染时

javascript - React useEffect 钩子(Hook)引用了错误的值

node.js - 在项目之间共享 react 组件的最简单方法是什么?

reactjs - react : create-react-app fails with error "Cannot find module ' libnpx'

javascript - 如何在React JS中获取文件的文件长度