Office2003以上,Word能夠以XML文本格式存儲,這樣就能夠使用外部程序建立Word文件,而不須要使用Word的對象。也可以自由的打開分析Word文件,或者發佈到本身的Web頁面,或者其餘更多應用。app
1)能夠是以下的樣子:框架
<?xml version="1.0"?>
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
dom
<w:body>
<w:p>
<w:r>
<w:t>Hello, World.</w:t>
</w:r>
</w:p>
</w:body>
</w:wordDocument>
能夠用記事本建立一個文件,將上面的XML內容粘貼,並保存爲helloworld.xml,在Office Word中打開它,就能看到如上圖所示的內容。ide
2)這是最簡單的WordXML內容,它包括這幾部分:
字體
XML的聲明和名稱空間的指明:
<?xml version="1.0"?>
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
ui
3)文檔內容spa
<w:body>…</w:body>code
從body內能夠看出,構成實際文本內容的有3中類型節點:
<w:p> 表示一個段落orm
<w:r> 表示一個樣式串,指明它包括的文本的顯示樣式xml
<w:t> 表示真正的文本內容
若是咱們須要指明一個文本爲粗體,須要怎麼辦呢?
<w:r>
<w:rPr>
<w:b w:val="on"/>
</w:rPr>
<w:t> 2.0C</w:t>
</w:r>
<w:b w:val=」on」> 表示該格式串種的文本爲粗體。
這樣,咱們就知道<w:r>表示一個特定的文本格式,稍微複雜點的格式:
<w:r>
<w:rPr>
<w:b w:val="on"/>
<w:sz w:val="40"/><w:szCs w:val="40"/>
<w:rFonts w:ascii="Arial" w:eastAsia="Arial" w:hAnsi="Arial" />
</w:rPr>
<w:t xml:space="preserve">2.0C</w:t>
</w:r>
字體爲粗體,尺寸爲是40除2等於20至關於幾號字體?,字體名稱「Arial」
<w:t xml:space="preserve"> 2.0C</w:t>
中的xml:space="preserve"從字面上理解是保持空格。
若是沒有這內容的話文本的先後空格將會被Word忽略。
若是咱們須要指定一個段的對齊方式,行距要怎麼辦呢?
這就要設置<w:p>的屬性了。相似於這樣:
<w:p>
<w:pPr>
<w:jc w:val="right"/>
<w:spacing w:line="600" w:lineRule="auto"/>
</w:pPr>
…
</w:p>
對齊方向:<w:jc w:val=」right」/> 這兒是右對齊。
行距:<w:spacing w:line=」600」 w:lineRule="auto"/> 600是用行距的倍數乘240得出,若是是兩倍行距,則是480。這兒應該是2.5倍行距。
因而可知,組裝一個WordXML格式的文件是一件比較簡單的事情。
將段屬性包含在<w:pPr></w:pPr>中
將文本格式包含在<w:rPr></w:rPr>中
這兒的Pr是property的意思,表示這個塊中是r(run)或p(paragraph)的格式設置。
一個WordXML的文件結束了嗎?能夠這樣講,但若是你雙擊剛纔建立的XML文件,有很大機會不會由Word來打開它。
這是爲何呢?
咱們還須要在合適的地方放置一條語句:
<?xml version="1.0"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument
用來指明這個xml文件的對應處理程序,對應註冊表中的鍵值:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\11.0\Common\Filter\text/xml
可是,加入這一條語句後,雙擊打開時,Word將會提示XML的格式不正確,雖然能打開。那是由於還有許多的內容沒有聲明。咱們就先不加這條語句。
下面內容設置了頁的寬,高,和頁的各邊距。各項的值均是英寸乘1440得出:
<w:body>…
<w:sectPr>
<w:pgSz w:w="12240" w:h="15840"/>
<w:pgMar w:top="1440" w:right="1800" w:bottom="1440" w:left="1800" w:header="720" w:footer="720" w:gutter="0"/>
</w:sectPr>
</w:body>
下面內容設置了頁的頁眉頁腳:
w:sectPr wsp:rsidR="002C452C">
<w:hdr w:type="odd" >
<w:p>
<w:pPr>
<w:pStyle w:val="Header"/>
</w:pPr>
<w:r>
<w:t>My Header</w:t>
</w:r>
</w:p>
</w:hdr>
<w:ftr w:type="odd">
<w:p>
<w:pPr>
<w:pStyle w:val="Footer"/>
</w:pPr>
<w:r>
<w:t>My Footer</w:t>
</w:r>
</w:p>
</w:ftr>
</w:sectPr>
</w:body>
這兩段都很直白,就不須要解釋了。
</w:body>
<w:docPr>
<w:view w:val="print"/><w:zoom w:percent="100"/>
</w:docPr>
</w:wordDocument>
docPr,就是document property的意思了。
表示文檔的視圖是「print」,視圖比例100%
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument xmlns:aml="http://schemas.microsoft.com/aml/2001/core"
xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:w10="urn:schemas-microsoft-com:office:word"
xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"
xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint"
xmlns:wsp="http://schemas.microsoft.com/office/word/2003/wordml/sp2"
xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core"
w:macrosPresent="no" w:embeddedObjPresent="no" w:ocxPresent="no"
xml:space="preserve">
<w:body>
<w:p>
<w:pPr>
<w:jc w:val="left"/>
<w:spacing w:line="240" w:lineRule="auto"/>
</w:pPr>
<w:r>
<w:rPr>
<w:sz w:val="24"/><w:szCs w:val="24"/>
<w:rFonts w:ascii="Arial" w:eastAsia="Arial" w:hAnsi="Arial" />
</w:rPr>
<w:t>Niu don't like Red or Blue! It seems that </w:t>
</w:r>
<w:r>
<w:rPr>
<w:sz w:val="48"/><w:szCs w:val="48"/>
<w:rFonts w:ascii="Arial" w:eastAsia="Arial" w:hAnsi="Arial" />
</w:rPr>
<w:t>Hello world!</w:t>
</w:r>
</w:p>
<w:sectPr wsp:rsidR="002C452C">
<w:pgSz w:w="12240" w:h="15840"/>
<w:pgMar w:top="1526.4" w:right="3254.4" w:bottom="2966.4" w:left="1670.4" w:header="720" w:footer="720" w:gutter="0"/>
<w:hdr w:type="odd" >
<w:p>
<w:pPr>
<w:pStyle w:val="Header"/>
</w:pPr>
<w:r>
<w:t>Header</w:t>
</w:r>
</w:p>
</w:hdr>
<w:ftr w:type="odd">
<w:p>
<w:pPr>
<w:pStyle w:val="Footer"/>
</w:pPr>
<w:r>
<w:t>Footer</w:t>
</w:r>
</w:p>
</w:ftr>
</w:sectPr>
</w:body>
<w:docPr>
<w:view w:val="print"/><w:zoom w:percent="100"/>
</w:docPr>
</w:wordDocument>
一、 "Office XML Handler" is the display name of the process MSOXMLED.EXE, which is locaded in C:\Program Files\Common Files\microsoft shared\OFFICE16 on my computer. It is used to open XML files (perhaps also other extentions?) and detect which MS Office program the file should be associated with (if any), and then open the file with that program.
This is explained in https://stackoverflow.com/a/1569619/1858923
二、 DOM4J是 dom4j.org 出品的一個開源 XML 解析包。DOM4J應用於 Java 平臺,採用了 Java 集合框架並徹底支持 DOM,SAX 和 JAXP。
三、 XSLT、XPath和DOM的應用研究
一、 "Office XML Handler" is the display name of the process MSOXMLED.EXE, which is locaded in C:\Program Files\Common Files\microsoft shared\OFFICE16 on my computer. It is used to open XML files (perhaps also other extentions?) and detect which MS Office program the file should be associated with (if any), and then open the file with that program.
This is explained in https://stackoverflow.com/a/1569619/1858923
一、 DOM4J是 dom4j.org 出品的一個開源 XML 解析包。DOM4J應用於 Java 平臺,採用了 Java 集合框架並徹底支持 DOM,SAX 和 JAXP。
二、 XSLT、XPath和DOM的應用研究