?网上好象关于如何构建支持名称空间的xml文档的资料似乎并不多 刚好啃过一下这方面的类容 也就借
Blog贴点,权当自己做了个笔记 .
?先简要介绍一下用到的类及其方法,更详细的介绍只有看java api文档了?????
DocumentBuilderFactory创建DOM解析器的工厂 调用其newInstance()方法实例化 然后用该实例创建DocumentBuilder
实例 该实例表示一个dom解析器????????
?DocumentBuilderFactory.setNamespaceAware(true); 提供对 XML 名称空间的支持。
DOMImplementation? 允许用户用根Element的限定名称和XML命名空间创建新Document
Document createDocument(String namespaceURI,String qualifiedName,???? DocumentType doctype)throws DOMException创建具有文档元素的指定类型的 DOM Document 对象。
? 参数:? namespaceURI - 要创建的文档元素的名称空间 URI 或 null。? qualifiedName - 要创建的文档元素的限定名称或 null。
? doctype - 要创建的文档的类型或 null。当 doctype 不是 null 时,其 Node.ownerDocument 属性将被设置为正在创建的文档。
返回:具有文档元素的新 Document 对象。如果 NamespaceURI、qualifiedName 和 doctype 为 null,则返回的 Document 为空的,不带有文档元素
Element getDocumentElement()这是一种便捷属性,该属性允许直接访问文档的文档元素的子节点。
Element createElementNS(String namespaceURI, String qualifiedName)? throws DOMException创建给定的限定名称和名称空间 URI 的元素。依据 [XML Namespaces],如果应用程序不希望使用名称空间,则必须将 null 作为方法的 namespaceURI 参数的值。
参数:namespaceURI - 要创建的元素的名称空间 URI。qualifiedName - 要实例化的元素类型的限定名称。 返回:具有以下属性的新 Element 对象: 属性 值 Node.nodeName qualifiedName Node.namespaceURI namespaceURI Node.prefix 前缀,从 qualifiedName 中提取的;如果没有前缀,则为 null? Node.localName 本地名称,从 qualifiedName 提取的 Element.tagName qualifiedName
Document对象的createAttributeNS Attr createAttributeNS(String namespaceURI,?????? String qualifiedName)?? throws DOMException创建给定的限定名称和名称空间 URI 的属性。对于每个 [XML Namespaces],如果应用程序希望没有名称空间,则它们必须将 null 作为方法的 namespaceURI 参数的值。 参数:namespaceURI - 要创建的属性的名称空间 URI。qualifiedName - 要实例化的属性的限定名称。
Element对象的setAttributeNS方法 void setAttributeNS(String namespaceURI, String qualifiedName,??? String value)??????????????? throws DOMException?? throws DOMException 添加新属性 namespaceURI - 要创建或更改的属性的名称空间 URI。qualifiedName - 要创建或更改的属性的限定名称。value - 以字符串形式设置的值。
import javax.xml.parsers.*;
import org.w3c.dom.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;
class BuilderXML {
??? public final static String SOAP_NS =
??????????? "http://schemas.xmlsoap.org/soap/envelop/";
??? public final static String MH_NS =
??????????? "http://com.163.hc/myxml";
??? public final static String XSD_NS = "http://www.w3c.org/2001/XMLSchema";
??? public final static String XSI_NS =
??????????? "http://www.w3c.org/2001/XMLSchema-instance";
??? public static void main(String[] args) throws Exception {
??????? BuilderXML builderxml = new BuilderXML();
??????? DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
???????
????
??????? factory.setNamespaceAware(true);
??????? DocumentBuilder builder = factory.newDocumentBuilder();
??????
??????? DOMImplementation doIml = builder.getDOMImplementation();
?
??????? Document doc = doIml.createDocument(SOAP_NS, "soap:Envelop", null);
???????
???????
??????? Element root = doc.getDocumentElement();
??????? root.setAttribute("xmlns:soap", SOAP_NS);
??????? root.setAttribute("xmlns:mh", MH_NS);
??????? root.setAttribute("xmlns:xsd", XSD_NS);
??????? root.setAttribute("xmlns:xsi", XSI_NS);
???????
??????? Element body = doc.createElementNS(SOAP_NS, "soap:Body");
??????? root.appendChild(body);
??????? Element getBookPrice = doc.createElementNS(MH_NS, "mh:getBookPrice");
??????? body.appendChild(getBookPrice);
??????? Element isbn = doc.createElementNS(MH_NS, "isbn");
??????? body.appendChild(isbn);
??????? Attr typeAttr = doc.createAttributeNS(XSI_NS, "xsi:type"); //attr 只能赋给element
??????? typeAttr.setValue("xsd:string");
??????? isbn.setAttributeNodeNS(typeAttr);
??????? isbn.setAttributeNS( MH_NS,"mh:tt","tt");
??????? //创建CDATA段
??????? CDATASection cdata=doc.createCDATASection("<hc>ttt</hc>");
????????? isbn.appendChild(cdata);
???????? // 创建文本
????????? Text tt = doc.createTextNode("made? two by hc");
????????? isbn.setNodeValue("made by hechang");
????????? isbn.appendChild(tt);
??????? Text text = doc.createTextNode("0311111");
??????? body.appendChild(text);
???????? //创建注释
??????? Comment comment = doc.createComment(" written by hc ");
??????? doc.insertBefore(comment,root);
???????? //创建转换器并将xml文档输出到输出流 如果输出流是一个输出文件流,则生成一个文件
??????? Transformer t = TransformerFactory.newInstance().newTransformer();
??????? t.setOutputProperty("indent", "yes"); //设置空白输出
??????? t.transform(new DOMSource(doc),
??????????????????? new StreamResult(System.out));
??? }
}
程序输出:
<soap:Envelop xmlns:soap="http://schemas.xmlsoap.org/soap/envelop/"
xmlns:mh="http://com.163.hc/myxml"
?xmlns:xsd="http://www.w3c.org/2001/XMLSchema"
?xmlns:xsi="http://www.w3c.org/2001/XMLSchema-instance">
<soap:Body>
<mh:getBookPrice/>
<isbn mh:tt="tt" xsi:type="xsd:string" xmlns="http://com.163.hc/myxml"><![CDATA[<hechang>ttt</hechang>]]>made? two by hechang</isbn>0311111</soap:Body>
</soap:Envelop>
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? 2006年7月3日