微软 WordXML格式初步分析

一、前言

Office2003以上,Word可以以XML文本格式存储,这样就可以使用外部程序创建Word文件,而不需要使用Word的对象。也能够自由的打开分析Word文件,或者发布到自己的Web页面,或者其他更多应用。

1、一个典型的WordXML结构

1)可以是如下的样子

<?xml version="1.0"?>
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
   <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中打开它,就能看到如上图所示的内容。

2)这是最简单的WordXML内容,它包括这几部分

XML的声明和名称空间的指明:
<?xml version="1.0"?>
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">

3)文档内容

<w:body>…</w:body>

2、基本节点类型

从body内可以看出,构成实际文本内容的有3中类型节点:
<w:p> 表示一个段落

<w:r> 表示一个样式串,指明它包括的文本的显示样式

<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_MACHINESOFTWAREMicrosoftOffice11.0CommonFilter ext/xml

但是,加入这一条语句后,双击打开时,Word将会提示XML的格式不正确,虽然能打开。那是因为还有许多的内容没有声明。我们就先不加这条语句。

 

3、页面设置

下面内容设置了页的宽,高,和页的各边距。各项的值均是英寸乘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>

4、页眉页脚

下面内容设置了页的页眉页脚:

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>

这两段都很直白,就不需要解释了。

5、文档设置

</w:body>

<w:docPr>
    <w:view w:val="print"/><w:zoom w:percent="100"/>
</w:docPr>

</w:wordDocument>

docPr,就是document property的意思了。

表示文档的视图是“print”,视图比例100%

二、完整的XML文件实例

<?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>

三、其他关注

1、   "Office XML Handler" is the display name of the process MSOXMLED.EXE, which is locaded in C:Program FilesCommon Filesmicrosoft sharedOFFICE16 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

2、   DOM4J dom4j.org 出品的一个开源 XML 解析包。DOM4J应用于 Java 平台,采用了 Java 集合框架并完全支持 DOMSAX JAXP

3、   XSLTXPathDOM的应用研究

1、   "Office XML Handler" is the display name of the process MSOXMLED.EXE, which is locaded in C:Program FilesCommon Filesmicrosoft sharedOFFICE16 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

1、   DOM4J dom4j.org 出品的一个开源 XML 解析包。DOM4J应用于 Java 平台,采用了 Java 集合框架并完全支持 DOMSAX JAXP

2、   XSLTXPathDOM的应用研究

原文地址:https://www.cnblogs.com/yxli2008/p/11073549.html