Java에서 내 XML의 각 요소 앞에 접두사를 붙일 수있는 방법이 있습니까? 이 작업을 수행 할 수있는 간단한 방법이 있을까요XML의 모든 요소에 접두사가 붙은 자바
<test:node1 xmlns test="www.test.com">
<test:node2>abc</test:node2>
<test:node3>
<test:node3_1>test1</test:node3_1>
</test:node3>
</test:node1>
:
<node1 xmlns="www.test.com">
<node2>abc</node2>
<node3>
<node3_1>test1</node3_1>
</node3>
</node1>
내가로 변환 할 예를 들어, 나는 다음과 같은 XML을 가지고? 지금까지 나는 시도했다 :
String messageString = message.getPayloadAsString();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
Document document = null;
try
{
builder = factory.newDocumentBuilder();
document = builder.parse(new InputSource(new StringReader(messageString)));
} catch (Exception e) {
e.printStackTrace();
}
XmlObject inboundMessageObject = XmlObject.Factory.parse(document);
XmlOptions xmlOptions = new XmlOptions();
Map<String, String> namespaceMap = new HashMap<String, String>();
namespaceMap.put("www.test.com", "test");
xmlOptions.setSaveSuggestedPrefixes(namespaceMap);
xmlObject.save(System.out, xmlOptions);
String prefixedXML = inboundMessageObject.xmlText(xmlOptions);
return prefixedXML;
접두어가 붙은 XXX 문자열은 messageString 문자열과 동일하다.
왜 기본'xmlns'가 이미 지정된 경우이 작업을 수행 할 것입니다 시도? 접두어를 추가하면 두 번째 예제에서 아무 것도 수행하지 않습니다. –
첫 번째 방법으로 XML을 보낼 때 우리 서버에있는 바보 같은 문제 때문에이 작업을 수행해야합니다. 지금까지이 시도했다 : XmlOptions xmlOptions = new XmlOptions(); // key = uri, value = prefix Map namespaceMap = new HashMap (); namespaceMap.put ("www.test.com", "test"); xmlOptons.setSaveSuggestedPrefixes (namespaceMap); xmlObject.save (System.out, xmlOptions); –
ridermule
앞으로는 (귀하를 위해 한 것처럼) 귀하의 질문에 실질적인 의견을 추가하십시오. 현재 코드가 작동하는지 여부를 설명 할 수 있습니까? –