2012-11-28 1 views
0

XML 파싱 라이브러리로 XOM을 사용하고 있습니다. 그리고 나는 이것을 XML 작성에도 사용하고 있습니다. 아래는 예제로 설명한 시나리오입니다.XOM으로 XML을 만드는 동안 코드 중복성 줄이기

시나리오 :

코드 :

Element root = new Element("atom:entry", "http://www.w3c.org/Atom"); 
Element city = new Element("info:city", "http://www.myinfo.com/Info"); 
city.appendChild("My City"); 
root.appendChild(city);  
Document d = new Document(root); 
System.out.println(d.toXML()); 

생성 된 XML : 여기 info 네임 스페이스 노드 자체에 추가되는 XML에

<?xml version="1.0"?> 
<atom:entry xmlns:atom="http://www.w3c.org/Atom"> 
    <info:city xmlns:info="http://www.myinfo.com/Info"> 
     My City 
    </info:city> 
</atom:entry> 

알 수 있습니다. 하지만 루트 요소에 추가해야합니다. 같은

<?xml version="1.0"?> 
<atom:entry xmlns:atom="http://www.w3c.org/Atom" xmlns:info="http://www.myinfo.com/Info"> 
    <info:city> 
     My City 
    </info:city> 
</atom:entry> 

아래 그리고이를 위해, 난 그냥

Element root = new Element("atom:entry", "http://www.w3c.org/Atom"); 
=> root.addNamespaceDeclaration("info", "http://www.myinfo.com/Info"); 
Element city = new Element("info:city", "http://www.myinfo.com/Info"); 
... ... ... 

문제는 내가 두 번 http://www.myinfo.com/Info를 추가했다 여기에 코드 조각 다음이 필요합니다. 그리고 제 경우에는 수백 개의 네임 스페이스가 있습니다. 그래서 너무 많은 redendancy가있을 것입니다. 이 중복성을 제거 할 수있는 방법이 있습니까?

답변

1

아니요,이 중복성을 제거 할 방법이 없으며 이는 신중한 결정입니다. XOM에서 네임 스페이스는 요소 자체의 기본 부분이며 문서에서의 위치 기능이 아닙니다.

물론 네임 스페이스 URI에 대해 명명 된 상수를 선언 할 수도 있습니다.