[英]namespace in java dom issue
我正在使用以下API創建具有名稱空間的Dom文檔,我的問題是為什么我需要將第一個參數“ http://www.w3.org/2000/xmlns/”設置為常量? 當我將null設置為錯誤時。 我問它,因為它太籠統了,那我為什么要放它呢?
rootTreeNode.setAttributeNS("http://www.w3.org/2000/xmlns/" ,"xmlns:m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
謝謝!
這是處理命名空間的方法:
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 ));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.