如何将 & 符号添加到 Word 2007 .docx XML 文件中?

发布于 2024-09-16 23:15:59 字数 403 浏览 3 评论 0原文

我正在根据客户输入生成 xml 格式的 Word 文档,当然,每当使用 & 时它就会崩溃。我尝试将 & 的所有实例替换为 &,但随后 & 确实出现在我的 Word 文档中。这是我的代码:

        static String replace(String in) {
            String ampersand = "&(?![a-zA-Z][a-zA-Z][a-zA-Z]?[a-zA-Z]?;)";
            return in.replaceAll(ampersand,"&");
        }

有什么想法吗?

I am generating a Word doc in xml based on customer input, and of course it blows up whenever an & is used. I tried replacing all instances of & with &, but then & literally shows up in my Word Doc. Here's my code:

        static String replace(String in) {
            String ampersand = "&(?![a-zA-Z][a-zA-Z][a-zA-Z]?[a-zA-Z]?;)";
            return in.replaceAll(ampersand,"&");
        }

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

狂之美人 2024-09-23 23:15:59

问题在于 TOC 和标题中 & 符号的转义方式不同。

在 TOC 中,它被转义为 &,而在标题中,它被转义为 \u0026

解决方案是在 TOC 和标题中以不同的方式转义 & 符号:

  • TOC: html 转义序列
  • TITLE: unicode 转义序列。

The problem is that the ampersand is escaped differently in TOC and the title.

In TOC, it is escaped as & while in the title is escaped as \u0026.

Solution is to escape ampersand differently in TOC and title:

  • TOC: html escaping sequence
  • TITLE: unicode escape sequence.
似梦非梦 2024-09-23 23:15:59

您是否尝试过用 && (unicode) 替换 & 符号?

Have you tried replacing the ampersand with & or & (unicode)?

万水千山粽是情ミ 2024-09-23 23:15:59

Word 2007 本身就是 &,所以你的问题一定是别的问题。

Word 2007 itself does &, so your problem must be something else.

一张白纸 2024-09-23 23:15:59

你的正则表达式对我来说似乎是错误的。你为什么不这样做:

    static String replace(String in) {
        return in.replace("&","&");
    }

Your regular expressions seems wrong to me. Why don't you just do:

    static String replace(String in) {
        return in.replace("&","&");
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文