2011-11-02 5 views
0

I이 루트 요소 전에 처리 명령 삽입 다음 코드 :직렬화 XML 처리 명령

Document doc = builder.parse(file); 

doc.insertBefore(
      doc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"annotation.xsl\""), 
      doc.getDocumentElement()); 
doc.insertBefore(doc.createProcessingInstruction("oxygen", "NVDLSchema=\"annotation.nvdl\""), 
      doc.getDocumentElement()); 

을 내가 직렬화하려면이 옵션을 사용

FileOutputStream fos = new FileOutputStream(new File(file.getAbsolutePath() + ".out")); 
DOMImplementationLS ls = (DOMImplementationLS) builder.getDOMImplementation(); 

LSOutput lso = ls.createLSOutput(); 
lso.setByteStream(fos); 
ls.createLSSerializer().write(doc, lso); 

fos.close(); 

출력 I로 얻으십시오 :

<?xml version="1.0" encoding="UTF-8"?> 
<fulltext-document>...</fulltext-document><?xml-stylesheet type="text/xsl" href="annotation.xsl"?><?oxygen NVDLSchema="annotation.nvdl"?> 

그러나 처리 지침을 가지고 있습니다. 전에 루트 요소. 아마도 DOM 3이 올바르지 않다고 확인했지만 (아래 참조) 모든 것이 정상적으로 보입니다. 제가 놓친 것이 있습니까? 모든 해결책을 환영합니다.

P. Java 1.6.0_27 DOM을 사용합니다. 위의 내용이 버그처럼 보이면 버그 리포트에 대한 링크를 환영합니다. 이 (하지만, 버그 리포트를 찾을 수 없습니다) 고정 버그 그래서

enter image description here

답변

2

Xerces에 2.11.0는, 예상되는 동작이있다.

LSSerializer를 사용하는 대신 JDK 버전을 사용해야하는 경우 ID 변환을 사용할 수 있습니다.

Transformer t = TransformerFactory.newInstance().newTransformer(); 
    t.transform(new DOMSource(doc), new StreamResult(fos); 

노드 순서가 유지됩니다.

+0

우아한 솔루션을 이용해 주셔서 감사합니다. –