How to insert non breaking space in word xml using java

Viewed 1226

I'd like to use non breaking spaces in my word XML file. I know, in plain Word I could use ctrl+shift+space; for this and this works fine. However, when I put those non breaking space through java, then it will set empty.:

My Sample Code as,

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

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

                            }
                        }
                    }
                }
            }
        }
    }
}

I want to add non breaking spaces.

And, If that any other way also acceptable.

So, if u know please let me know..

Thanks,.

1 Answers

A no-break-space isn't a "normal" character you'd find on any keyboard...

To place this within XML you can

  • store the whole part as unicode and embedd it
  • use the character code U+00A0
  • use the dec HTML entity &#160;
  • use the hex HTML entity &#xa0;
  • use the named entity &nbsp;

There are various no-break-space characters (narrow, zero-width).

Find details here

Related