<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Author>Arthur </Author>
<LastAuthor>Arthur </LastAuthor>
<Revision>2 </Revision>
<TotalTime>1 </TotalTime>
<Created>2008-03-16T01:55:00Z </Created>
<LastSaved>2008-03-16T01:55:00Z </LastSaved>
<Company>sdau </Company>
<Version>12.00 </Version>
</DocumentProperties>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>5250 </WindowHeight>
<WindowWidth>8415 </WindowWidth>
<WindowTopX>240 </WindowTopX>
<WindowTopY>120 </WindowTopY>
<ProtectStructure>False </ProtectStructure>
<ProtectWindows>False </ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Center"/>
<Borders/>
<Font ss:FontName="宋体" x:CharSet="134" ss:Size="11" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s65">
<Alignment ss:Horizontal="Justify" ss:Vertical="Center" ss:WrapText="1"/>
<Font ss:FontName="宋体" x:CharSet="134" ss:Size="16" ss:Color="#000000"
ss:Bold="1" ss:Italic="1"/>
</Style>
</Styles>
<Worksheet ss:Name="a.">
<Table ss:ExpandedColumnCount="7" ss:ExpandedRowCount="1" x:FullColumns="1"
x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="13.5">
<Row ss:AutoFitHeight="0" ss:Height="20.25">
<Cell ss:MergeAcross="6" ss:StyleID="s65"> <Data ss:Type="String">宋体三号加粗倾斜 </Data> </Cell>
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<Print>
<ValidPrinterInfo/>
<PaperSizeIndex>0 </PaperSizeIndex>
<VerticalResolution>0 </VerticalResolution>
<NumberofCopies>0 </NumberofCopies>
</Print>
<Selected/>
<DoNotDisplayGridlines/>
<Panes>
<Pane>
<Number>3 </Number>
<RangeSelection>R1C1:R1C7 </RangeSelection>
</Pane>
</Panes>
<ProtectObjects>False </ProtectObjects>
<ProtectScenarios>False </ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>
上边是一个由word文件转换出来的xml文件,现在我想遍历Style样式名,找到对应StyleID的文本内容,文本格式等信息,如StyleID="s65",我就应该能得到文本内容是“宋体三号加粗倾斜”,FontName为宋体等等。希望可以用JDOM实现,望达人门指教
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Author>Arthur </Author>
<LastAuthor>Arthur </LastAuthor>
<Revision>2 </Revision>
<TotalTime>1 </TotalTime>
<Created>2008-03-16T01:55:00Z </Created>
<LastSaved>2008-03-16T01:55:00Z </LastSaved>
<Company>sdau </Company>
<Version>12.00 </Version>
</DocumentProperties>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>5250 </WindowHeight>
<WindowWidth>8415 </WindowWidth>
<WindowTopX>240 </WindowTopX>
<WindowTopY>120 </WindowTopY>
<ProtectStructure>False </ProtectStructure>
<ProtectWindows>False </ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Center"/>
<Borders/>
<Font ss:FontName="宋体" x:CharSet="134" ss:Size="11" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s65">
<Alignment ss:Horizontal="Justify" ss:Vertical="Center" ss:WrapText="1"/>
<Font ss:FontName="宋体" x:CharSet="134" ss:Size="16" ss:Color="#000000"
ss:Bold="1" ss:Italic="1"/>
</Style>
</Styles>
<Worksheet ss:Name="a.">
<Table ss:ExpandedColumnCount="7" ss:ExpandedRowCount="1" x:FullColumns="1"
x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="13.5">
<Row ss:AutoFitHeight="0" ss:Height="20.25">
<Cell ss:MergeAcross="6" ss:StyleID="s65"> <Data ss:Type="String">宋体三号加粗倾斜 </Data> </Cell>
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<Print>
<ValidPrinterInfo/>
<PaperSizeIndex>0 </PaperSizeIndex>
<VerticalResolution>0 </VerticalResolution>
<NumberofCopies>0 </NumberofCopies>
</Print>
<Selected/>
<DoNotDisplayGridlines/>
<Panes>
<Pane>
<Number>3 </Number>
<RangeSelection>R1C1:R1C7 </RangeSelection>
</Pane>
</Panes>
<ProtectObjects>False </ProtectObjects>
<ProtectScenarios>False </ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>
上边是一个由word文件转换出来的xml文件,现在我想遍历Style样式名,找到对应StyleID的文本内容,文本格式等信息,如StyleID="s65",我就应该能得到文本内容是“宋体三号加粗倾斜”,FontName为宋体等等。希望可以用JDOM实现,望达人门指教
package bankemhr;
import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;
import java.io.*;
import java.util.List;
public class JDomeSample
{
public static void main(String[] args) throws Exception // 如果有任何异常则抛出
{
SAXBuilder sb = new SAXBuilder(); // 新建立构造器
Document doc = sb.build(new FileInputStream("E:/program/bankemhr/1.xml")); // 读入1.xml Element root = doc.getRootElement(); // 取得根节点, 就是例子中的<total>节点
List list = root.getChildren(); // 取得根节点下一层所有节点放入List类中 for(int i=0; i<list.size(); i++)
{
System.out.println("-------------------------");
Element item = (Element)list.get(i); // 取得节点实例
String name = item.getAttribute("name").getValue(); // 取得属性的值
System.out.println("NAME-->"+name); Element sub = item.getChild("sub"); // 取得当前节点的指定子节点
String text = sub.getText(); // 取得指定子节点的内容
System.out.println("SUB-->"+text);
sub.setText("new item"+String.valueOf(i) ); // 改变子节点的内容
}
Element item = (Element)list.get(0); // 取得根节点下第一个子节点 Attribute a = new Attribute("started","true"); // 增加一个新的属性
item.setAttribute(a);
//item.addAttribute(a);
item.setAttribute("name","new item"); // 改变旧的属性值
String indent = ""; // 缩进符号
boolean newLines = false; // 是否产生新行
XMLOutputter outp = new XMLOutputter (indent,newLines,"gb2312"); // 构造新的输出流
outp.output(doc, new FileOutputStream("E:/program/bankemhr/2.xml")); // 输出到2.XML文件中
}
}
那么用dom来解就简单多了。
1 将XML读入一个dom对相
2 获取ROOT
3 获取ROOT的儿子Styles
4 获取Styles的儿子们,并找到ss:ID为指定条件的儿子
5 解读该节点