2017-06-22 15 views
1

以下のXML文書を作成しようとしています。私はどのように名前空間が、を使用する必要があります知っている属性xsiを持つXMLノードを作成:JDOM2の型

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

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(); 
     } 
    } 
} 

しかし、私は以下のエラーを取得しています -

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

は、私は以下のようにJavaコードを使用しています私は理解できません。

答えて

1

JDOMは、あなたが原因の名前空間に予約されている:上のXML 1.0仕様にコロン(:)その方法を、含むパスを作成することはできません。 Check JDOM's FAQ here.

名前空間を使用する属性を設定または作成するには、名前空間をパラメータとして受け入れる関数/コンストラクタを使用する必要があります。

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

UPDATE:

私たちは、子供の両親のいずれかの内部の名前空間宣言(フィールド)を追加し、設定することができます。この場合

は、次のように使用することができます指定された属性に対してこの名前空間を使用するようにします。

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> 

この機能NamespaceAware Interface


なぜget(2)の一部です。のリストルート要素には、以下の文章で説明し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私は答えを更新しましたが、あなたに通知するのを忘れました –

関連する問題