dom 4 jがxmlを生成する場合、属性値の中の戻り改行問題


まず属性の折り返し行は私にとってとても役に立ちますが、使用中にdom 4 jが自動的に折り返し行を外していることに気づきました.
生成する必要があるxmlの部分はこうです.

  @H=16*16
  @C=60
私の中国
  @I=000
  @K=2
  @O=000
  @Q

xmlの部分コードを生成する

try {
    StringWriter writer = new StringWriter();
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("GB2312");
    XMLWriter xmlwriter = new XMLWriter(writer, format);
    xmlwriter.write(document);
    returnValue = writer.toString();
     } catch (Exception ex) {
       ex.printStackTrace();
   }

上記のコードでxmlを生成した結果は
@H=16*16@C=60私の中国@I=000@K=2@O=000@Q
xmlを生成する前のコードに折り返し改行があることを確認した後、「dom 4 jの問題に違いない」と詳しく考えた.
問題はここにあるcreatePrettyPrint();こちらへ.

 /**
     * A static helper method to create the default pretty printing format. This
     * format consists of an indent of 2 spaces, newlines after each element and
     * all other whitespace trimmed, and XMTML is false.
     * 
     * @return DOCUMENT ME!
     */
    public static OutputFormat createPrettyPrint() {
        OutputFormat format = new OutputFormat();
        format.setIndentSize(2);
        format.setNewlines(true);
        format.setTrimText(true);
        format.setPadText(true);

        return format;
    }

    /**
     * A static helper method to create the default compact format. This format
     * does not have any indentation or newlines after an alement and all other
     * whitespace trimmed
     * 
     * @return DOCUMENT ME!
     */
    public static OutputFormat createCompactFormat() {
        OutputFormat format = new OutputFormat();
        format.setIndent(false);
        format.setNewlines(false);
        format.setTrimText(true);

        return format;
    }
}

createPrettyPrint()メソッドには
format.setTrimText(true);
問題はここにある.
新しいクラスMyOutputFormatはOutputFormatを継承しcreatePrettyPrint()メソッドを再ロードしました

public class MyOutputFormat extends OutputFormat {
	 public static OutputFormat createPrettyPrint() { 
	        OutputFormat format = new OutputFormat(); 
	        format.setIndentSize(2); 
	        format.setNewlines(true); 
	        format.setTrimText(false); 
	        format.setPadText(true); 

	        return format; 
	    } 

}

修正後のコード

try {
    StringWriter writer = new StringWriter();
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("GB2312");
    XMLWriter xmlwriter = new XMLWriter(writer, format);
    xmlwriter.write(document);
    returnValue = writer.toString();
     } catch (Exception ex) {
       ex.printStackTrace();
   }

最終的な問題解決