2016-03-04 47 views
2

下面是輸入和預期的輸出xml。我用JDOM解析器如何將命名空間添加到以下xml?

輸入:

<emp:transactions xmlns:emp="http://www.emp.com/dept" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=""> 
    <emp:transaction xmlns:dept="http://www.emp.com/emp"> 
     <emp:empBooking emp:bookingNbr=""/> 
    </emp:transaction> 
</emp:transactions> 

預期輸出:

<emp:transactions xmlns:emp="http://www.emp.com/dept" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=""> 
    <emp:transaction xmlns:dept="http://www.emp.com/emp"> 
     <emp:empBooking emp:bookingNbr=""/> 
     <emp:flexStringFields> 
      <emp:flexString01>emp flex 1</emp:flexString01> 
     </emp:flexStringFields> 
    </emp:transaction> 
</emp:transactions> 

代碼:

SAXBuilder builder = new SAXBuilder(); 
Reader in = new StringReader(xmlAsString); 
Document document = (Document)builder.build(in); 
Element rootNode = document.getRootElement(); 
List<Element> list = rootNode.getChildren (
    "transaction", 
    Namespace.getNamespace("NS1", "http://www.emp.com/dept") 
); 
for(Element transaction: list) { 
    // ... 
} 

我不知道如何將元素emp:flexStringFields有加名字空間emp及其子女emp:flexString01

請問如何添加它?

+0

的可能的複製[?我如何添加一個命名空間前綴到XML DOM對象(http://stackoverflow.com/questions/10995338/how-do- I-添加-A-命名空間前綴到一個-XML的DOM對象) – har07

回答

0

下正常工作:

Element flexStringField = new Element (
    "flexStringFields", 
    "emp", 
    Namespace.getNamespace("NS1", "http://www.emp.com/dept").getURI() 
); 
content.addContent(flexStringField);