gpt4 book ai didi

java - java dom 中的命名空间问题

转载 作者:太空宇宙 更新时间:2023-11-04 08:00:00 25 4
gpt4 key购买 nike

我正在使用以下 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 ));
}

关于java - java dom 中的命名空间问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12991074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com