?網(wǎng)上好象關(guān)于如何構(gòu)建支持名稱空間的xml文檔的資料似乎并不多 剛好啃過一下這方面的類容 也就借
Blog貼點(diǎn),權(quán)當(dāng)自己做了個(gè)筆記 .
?先簡(jiǎn)要介紹一下用到的類及其方法,更詳細(xì)的介紹只有看java api文檔了?????
DocumentBuilderFactory創(chuàng)建DOM解析器的工廠 調(diào)用其newInstance()方法實(shí)例化 然后用該實(shí)例創(chuàng)建DocumentBuilder
實(shí)例 該實(shí)例表示一個(gè)dom解析器????????
?DocumentBuilderFactory.setNamespaceAware(true); 提供對(duì) XML 名稱空間的支持。
DOMImplementation? 允許用戶用根Element的限定名稱和XML命名空間創(chuàng)建新Document
Document createDocument(String namespaceURI,String qualifiedName,???? DocumentType doctype)throws DOMException創(chuàng)建具有文檔元素的指定類型的 DOM Document 對(duì)象。
? 參數(shù):? namespaceURI - 要?jiǎng)?chuàng)建的文檔元素的名稱空間 URI 或 null。? qualifiedName - 要?jiǎng)?chuàng)建的文檔元素的限定名稱或 null。
? doctype - 要?jiǎng)?chuàng)建的文檔的類型或 null。當(dāng) doctype 不是 null 時(shí),其 Node.ownerDocument 屬性將被設(shè)置為正在創(chuàng)建的文檔。
返回:具有文檔元素的新 Document 對(duì)象。如果 NamespaceURI、qualifiedName 和 doctype 為 null,則返回的 Document 為空的,不帶有文檔元素
Element getDocumentElement()這是一種便捷屬性,該屬性允許直接訪問文檔的文檔元素的子節(jié)點(diǎn)。
Element createElementNS(String namespaceURI, String qualifiedName)? throws DOMException創(chuàng)建給定的限定名稱和名稱空間 URI 的元素。依據(jù) [XML Namespaces],如果應(yīng)用程序不希望使用名稱空間,則必須將 null 作為方法的 namespaceURI 參數(shù)的值。
參數(shù):namespaceURI - 要?jiǎng)?chuàng)建的元素的名稱空間 URI。qualifiedName - 要實(shí)例化的元素類型的限定名稱。 返回:具有以下屬性的新 Element 對(duì)象: 屬性 值 Node.nodeName qualifiedName Node.namespaceURI namespaceURI Node.prefix 前綴,從 qualifiedName 中提取的;如果沒有前綴,則為 null? Node.localName 本地名稱,從 qualifiedName 提取的 Element.tagName qualifiedName
Document對(duì)象的createAttributeNS Attr createAttributeNS(String namespaceURI,?????? String qualifiedName)?? throws DOMException創(chuàng)建給定的限定名稱和名稱空間 URI 的屬性。對(duì)于每個(gè) [XML Namespaces],如果應(yīng)用程序希望沒有名稱空間,則它們必須將 null 作為方法的 namespaceURI 參數(shù)的值。 參數(shù):namespaceURI - 要?jiǎng)?chuàng)建的屬性的名稱空間 URI。qualifiedName - 要實(shí)例化的屬性的限定名稱。
Element對(duì)象的setAttributeNS方法 void setAttributeNS(String namespaceURI, String qualifiedName,??? String value)??????????????? throws DOMException?? throws DOMException 添加新屬性 namespaceURI - 要?jiǎng)?chuàng)建或更改的屬性的名稱空間 URI。qualifiedName - 要?jiǎng)?chuàng)建或更改的屬性的限定名稱。value - 以字符串形式設(shè)置的值。
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 =
??????????? "??? public final static String MH_NS =
??????????? "??? public final static String XSD_NS = "??? public final static String XSI_NS =
??????????? "??? 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");
??????? //創(chuàng)建CDATA段
??????? CDATASection cdata=doc.createCDATASection("<hc>ttt</hc>");
????????? isbn.appendChild(cdata);
???????? // 創(chuàng)建文本
????????? Text tt = doc.createTextNode("made? two by hc");
????????? isbn.setNodeValue("made by hechang");
????????? isbn.appendChild(tt);
??????? Text text = doc.createTextNode("0311111");
??????? body.appendChild(text);
???????? //創(chuàng)建注釋
??????? Comment comment = doc.createComment(" written by hc ");
??????? doc.insertBefore(comment,root);
???????? //創(chuàng)建轉(zhuǎn)換器并將xml文檔輸出到輸出流 如果輸出流是一個(gè)輸出文件流,則生成一個(gè)文件
??????? Transformer t = TransformerFactory.newInstance().newTransformer();
??????? t.setOutputProperty("indent", "yes"); //設(shè)置空白輸出
??????? t.transform(new DOMSource(doc),
??????????????????? new StreamResult(System.out));
??? }
}
程序輸出:
<soap:Envelop xmlns:soap="xmlns:mh="?xmlns:xsd="?xmlns:xsi="<soap:Body>
<mh:getBookPrice/>
<isbn mh:tt="tt" xsi:type="xsd:string" xmlns="? two by hechang</isbn>0311111</soap:Body>
</soap:Envelop>
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? 歡迎加入QQ群:30406099?