1

私は次の API を使用して名前空間を持つ Dom ドキュメントを作成していますが、最初の引数 "http://www.w3.org/2000/xmlns/" を配置する必要があるのはなぜですか? null を入れると error が発生します。一般的すぎるので質問しますが、なぜそれを置く必要があるのですか?

rootTreeNode.setAttributeNS("http://www.w3.org/2000/xmlns/" ,"xmlns:m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");

ありがとう!

4

1 に答える 1

1

名前空間を処理する方法は次のとおりです。

public static void main( String[] args ) throws Throwable {
   DocumentBuilderFactory dbf  = DocumentBuilderFactory.newInstance();
   dbf.setNamespaceAware( true );

   DocumentBuilder db = dbf.newDocumentBuilder();
   Document doc = db.newDocument();

   Element root = doc.createElement( "root" );
   root.setAttribute( "xmlns:m" , "http://www.lfinance.fr/blog-rachat-credits" );
   root.setAttribute( "xmlns:rt", "http://www.lfinance.fr/forum-rachat-credits" );
   doc.appendChild( root );

   Element elt = doc.createElement( "simple" );
   elt.setAttribute( "m:FC_TargetPath"   , "false" );
   elt.setAttribute( "m:FC_KeepInContent", "false" );
   elt.setAttribute( "rt:filterable"     , "false" );

   root.appendChild( doc.createTextNode( "\n\t" ));
   root.appendChild( elt );
   root.appendChild( doc.createTextNode( "\n" ));
   TransformerFactory.newInstance().newTransformer().transform(
      new DOMSource( doc ),
      new StreamResult( System.out ));
}
于 2012-10-20T18:19:50.930 に答える