Document doc = new Document();
Element root = new Element("root");
Namespace xmlns = Namespace.getNamespace("xsi", "http://www.gggggg");
root.addNamespaceDeclaration(xmlns);
root.setNamespace(Namespace.getNamespace("http://www.kkkkkk"));
Element a = new Element("a");
a.setNamespace(Namespace.NO_NAMESPACE);
root.addContent(a);//root element 의 하위 element 를 만들기
a.setText("문자"); // a element에 text 넣기
a.setAttribute("id", "01"); // a태그의 속성값 넣기
Element b = new Element("b");
root.addContent(b);
b.setText("문자bbb");
doc.setRootElement(root);
FileOutputStream fos = null;
File file = null;
try{
file = new File("d:\\test.xml");
fos = new FileOutputStream(file);
XMLOutputter serializer = new XMLOutputter();
Format f = serializer.getFormat();
f.setEncoding("UTF-8");
//encoding 타입을 UTF-8 로 설정
f.setIndent(" ");
f.setLineSeparator("\r\n");
f.setTextMode(Format.TextMode.TRIM);
serializer.setFormat(f);
serializer.output(doc, fos);
}
catch(Exception e){
return null;
}finally{
try{ if(fos!=null) fos.close(); }catch(IOException ignore){}
}
[결과]
헤더에
<?xml-stylesheet type="Text/xsl" href="PCTExmnSrh_6.xsl"?>
를 걸고 싶다면
Map<String, String> m = new HashMap<String, String>();
m.put("href", "PCTExmnSrh_6.xsl");
m.put("type", "Text/xsl");
doc.addContent(0, new ProcessingInstruction("xml-stylesheet", m));
를 추가 하면 된다.
이런 방식으로 하면 모든 태그들이 잘 나온다..
하지만 풀지못한 의문점이 있는데 root 태그에 xmlns="" 를 걸면
하위 태그까지 xmlns가 따라온다
여기에 a의 하위태그를 걸면 여기에는 xmlns가 생기지 않는다.
아시는분...댓글좀 달아주세요 ㅠㅠ
자체 해결 하였습니다. 이것저것 해보다가 얻어걸렸네요^^ 이거 해결하려고 쫌 고생했습니다..
위의 코드에서
a.setNamespace(Namespace.getNamespace("http://www.kkkkkk"));
b.setNamespace(Namespace.getNamespace("http://www.kkkkkk"));
이렇게 상위 네임스페이스의 URI를 일치시켜주면 됩니다. 하위 태그들은 부모의 네임스페이스를 자동으로 상속받는다고 하더군요...
'개발 > java,spring' 카테고리의 다른 글
| 에러 : Missing artifact com.oracle:ojdbc14:jar:10.2.0.4.0 (0) | 2016.04.28 |
|---|---|
| [java]자바 스프링, spring AOP 구현 (xml 방식) (0) | 2016.04.21 |
| [java spring]spring AOP 구현(Annotation 방법) (0) | 2016.04.16 |
| [spring]스프링 aop 개념 (0) | 2016.04.16 |
| xls, xlsx 파싱(XSSFWorkbook, HSSFWorkbook, XLSParser) (0) | 2016.04.16 |
| [java]자바 파일 삭제 (0) | 2016.04.06 |
| [java]자바 파일 복사 (4) | 2016.03.31 |