any フィールドに設定されたデータの取得は、名前が分かっている場合、他のすべてのエレメント値と同じように行うことができます。
XPath の「<name>」で get を行うことができ、それは解決されます。名前が不明の場合は、上記のようにインスタンス・プロパティーを調べることにより、値を見つけることができます。複数の any タグが存在するか、maxOccurs > 1 の any タグが存在する場合、データの発生元である any タグを特定することが重要であるなら、DataObject sequence を代わりに使用する必要があります。
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://AnyElemAny" targetNamespace="http://AnyElemAny"> <xsd:complexType name="AnyElemAny"> <xsd:sequence> <!-- Handle all these any one way --> <xsd:any maxOccurs="3"/> <xsd:element name="marker1" type="xsd:string"/> <!-- Handle this any in another --> <xsd:any/> </xsd:sequence> </xsd:complexType> </xsd:schema>
<any/> タグは DataObject を順序付けするので、どの any 値が設定されたかの判別は、Sequence 内の any プロパティーの位置を調べることによって行うことができます。
以下の XSD のインスタンス・データが属する any タグを判別するには、以下のコードを使用します。
DataObject anyElemAny = ... Seqeuence seq = anyElemAny.getSequence(); // marker1 エレメントを検出するまで、見つかったすべてのオープン・データは // 最初の any タグに属する boolean foundMarker1 = false; for (int i=0; i<seq.size(); i++) { Property prop = seq.getProperty(i); // プロパティーがオープン・プロパティーであるかどうかをチェックする if (prop.isOpenContent()) { if (!foundMarker1) { // 最初の any でなければならない // marker1 エレメントの前にあるからである System.out.println("Found first any data: "+seq.getValue(i)); } else { // 2 番目の any でなければならない // marker1 エレメントの後にあるからである System.out.println("Found second any data: "+seq.getValue(i)); } } else { // marker1 エレメントでなければならない System.out.println("Found marker1 data: "+seq.getValue(i)); foundMarker1 = true; } }
<any/> 値の設定は、グローバル・エレメント・プロパティーを作成し、その値をシーケンスに追加することによって行います。
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://GlobalElems" targetNamespace="http://GlobalElems"> <xsd:element name="globalElement1" type="xsd:string"/> <xsd:element name="globalElement2" type="xsd:string"/> </xsd:schema> DataObject anyElemAny = ... Seqeuence seq = anyElemAny.getSequence(); // globalElement1 のグローバル・エレメント Property を取得する Property globalProp1 = boXsdHelper.getGlobalProperty(http://GlobalElems, "globalElement1", true); // globalElement2 のグローバル・エレメント Property を取得する Property globalProp2 = boXsdHelper.getGlobalProperty(http://GlobalElems, "globalElement2", true); // データを最初の any のシーケンスに追加する seq.add(globalProp1, "foo"); seq.add(globalProp1, "bar"); // marker1 のデータを追加する seq.add("marker1", "separator"); // または anyElemAny.set("marker1", "separator") // データを 2 番目の any のシーケンスに追加する seq.add(globalProp2, "baz"); // これで、get 呼び出しによってデータにアクセスできる System.out.println(dobj.get("globalElement1"); // 「[foo, bar]」を表示する System.out.println(dobj.get("marker1"); // 「separator」を表示する System.out.println(dobj.get("globalElement2"); // 「baz」を表示する