2017-06-22 53 views
1

我正在嘗試創建以下XML文檔。使用屬性xsi創建XML節點:在JDOM2中鍵入

<?xml version="1.0" encoding="UTF-8"?> 
<BCPFORMAT> 
    <RECORD> 
    <FIELD ID="1" xsi:type="CharFixed" MAX_LENGTH="4" /> 
    </RECORD> 
</BCPFORMAT> 

我使用Java代碼如下 -

package com.tutorialspoint.xml; 

import java.awt.List; 
import java.io.File; 
import java.io.FileWriter; 
import java.util.ArrayList; 

import javax.xml.transform.Transformer; 
import javax.xml.transform.TransformerFactory; 
import javax.xml.transform.stream.StreamResult; 

import org.jdom2.Document; 
import org.jdom2.Element; 
import org.jdom2.output.Format; 
import org.jdom2.output.XMLOutputter; 

public class createXmlLayout { 
    public static void main(String[] args) { 
     Document doc = new Document(); 
     Element root = new Element("BCPFORMAT"); 
     //RECORD Element 
     Element child = new Element("RECORD"); 
     //FIELD Element 
     Element name = new Element("FIELD") 
       .setAttribute("ID", "1") 
       .setAttribute("xsi:type", "CharFixed") 
       .setAttribute("MAX_LENGTH", "4"); 

     child.addContent(name); 
     root.addContent(child); 
     doc.addContent(root); 

     XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()); 
     try { 
      outputter.output(doc, System.out); 
      outputter.output(doc, new FileWriter("c:\\VTG_MAPN.xml")); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

但我發現了以下錯誤:

The name "xsi:type" is not legal for JDOM/XML attributes: XML name 'xsi:type' cannot contain the character ":".

我知道我可能需要使用命名空間,但如何我無法弄清楚。

回答

1

JDOM不允許對:創建包含冒號(:)這樣的路徑,由於XML 1.0規範被保留到命名空間。 Check JDOM's FAQ here.

要設置或創建使用命名空間的屬性,必須使用接受命名空間作爲參數的函數/構造函數。

在這種情況下,你可以使用以下命令:

e.setAttribute("type", "CharFixed", Namespace.getNamespace("xsi", "xsi_uri")); 

UPDATE:

我們可以添加一個命名空間聲明中的子(FIELD)的父母一方,並設置子對給定的屬性使用這個命名空間。

Namespace namespace = Namespace.getNamespace("xsi", "xsi_uri"); 
root.addNamespaceDeclaration(namespace); 
// ... 

Element name = new Element("FIELD") 
     .setAttribute("ID", "1") 
     .setAttribute("type", "CharFixed", root.getNamespacesInScope().get(2)) 
     .setAttribute("MAX_LENGTH", "4"); 

// ... 

輸出將是如下:

<?xml version="1.0" encoding="UTF-8"?> 
<BCPFORMAT xmlns:xsi="xsi_uri"> 
    <RECORD> 
    <FIELD ID="1" xsi:type="CharFixed" MAX_LENGTH="4" /> 
    </RECORD> 
</BCPFORMAT> 

此功能

爲什麼get(2)NamespaceAware Interface


部分:

如果我們繼承的命名空間名單根元素它將返回3名的命名空間,由下面的文本中描述:

[Namespace: prefix "" is mapped to URI ""] 
[Namespace: prefix "xml" is mapped to URI "http://www.w3.org/XML/1998/namespace"] 
[Namespace: prefix "xsi" is mapped to URI "xsi_uri"] 

因此,索引0是一個空的命名空間,索引1是默認的XML命名空間,最後,索引2是用於的xsi添加的命名空間。


當然,我們不希望硬編碼索引所需的命名空間,因此,我們可以做到以下幾點緩存事先所期望的命名空間:

Namespace xsiNamespace = 
     root.getNamespacesInScope().stream()  // Streams the namespaces in scope 
     .filter((ns)->ns.getPrefix().equals("xsi")) // Search for a namespace with the xsi prefix 
     .findFirst()        // Stops at the first find 
     .orElse(namespace);       // If nothing was found, returns 
                // the previously declared 'namespace' object instead. 

使用緩存的命名空間:

// ... 
.setAttribute("type", "CharFixed", xsiNamespace) 
// ... 
+0

的輸出來爲:我們可以刪除的xmlns:的xsi = 「xsi_uri」 ????它應該是:

+0

@SumantraChakraborty我更新了答案但忘了通知你 –