如何使用java在word xml中插入不间断空格

如何使用java在word xml中插入不间断空格,java,html,xml,parsing,dom,Java,Html,Xml,Parsing,Dom,我希望在word XML文件中使用不间断空格。我知道,简单地说,我可以使用ctrl+shift+space;这个和这个都很好。但是,当我通过java放入这些非中断空间时,它将设置为空: 我的示例代码为 public static void main(String[] args) { String readpath = "D:/Sathish/TestDocuments/ReadDocument/temp/final/Testing.xml"; Document doc = rea

我希望在word XML文件中使用不间断空格。我知道,简单地说,我可以使用ctrl+shift+space;这个和这个都很好。但是,当我通过java放入这些非中断空间时,它将设置为空:

我的示例代码为

public static void main(String[] args) {
    String readpath = "D:/Sathish/TestDocuments/ReadDocument/temp/final/Testing.xml";
    Document doc = readDocument(readpath);
    NodeList nList = doc.getElementsByTagName("w:body");
    Node nNode = nList.item(0);
    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
        Element eElement = (Element) nNode;
        NodeList wpList = eElement.getElementsByTagName("w:p");
        String tag = "";
        for (int temp = 0; temp < wpList.getLength(); temp++) {
            Node nWpNode = wpList.item(temp);
            if (nWpNode.getNodeType() == Node.ELEMENT_NODE) {
                Element wpElement = (Element) nWpNode;
                NodeList wrNodeList = wpElement.getChildNodes();
                for(int j = 0; j < wrNodeList.getLength(); j++){
                    Node nWrNode = wrNodeList.item(j);
                    if (nWrNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element wrElement = (Element) nWrNode;
                        NodeList nodeList = wrElement.getElementsByTagName("w:t");
                        for(int temp1 = 0; temp1 < nodeList.getLength(); temp1++){
                            Node nWtNode = nodeList.item(temp1);
                            if (nWtNode.getNodeType() == Node.ELEMENT_NODE) {
                                Element wtElement = (Element) nWtNode;
                                tag = wtElement.getTextContent();
                                //here, matching Conditions..

                            // It will be set Normal Space  
                                wtElement.setAttribute("xml:space", "preserve");
                                wtElement.setTextContent(" ");

                            // I need to set non-breaking space

                            //  ??????????????????????

                            }
                        }
                    }
                }
            }
        }
    }
}
publicstaticvoidmain(字符串[]args){
String readpath=“D:/Sathish/TestDocuments/ReadDocument/temp/final/Testing.xml”;
Document doc=readDocument(readpath);
NodeList nList=doc.getElementsByTagName(“w:body”);
节点nNode=nList.item(0);
if(nNode.getNodeType()==Node.ELEMENT\u Node){
元素EEElement=(元素)nNode;
NodeList wpList=eeelement.getElementsByTagName(“w:p”);
字符串标签=”;
对于(int-temp=0;temp
我想添加不间断的空格

如果有其他方式也可以接受的话

所以,如果你知道,请让我知道

谢谢。

无中断空格不是任何键盘上都能找到的“正常”字符

要将其放在XML中,您可以

  • 将整个部件存储为unicode并嵌入其中
  • 使用字符代码
    U+00A0
  • 使用dec HTML实体
     
  • 使用十六进制HTML实体
     
  • 使用命名实体
有各种无换行符字符(窄、零宽度)

发现