【发布时间】:2016-01-05 00:35:52
【问题描述】:
请帮我用Java解析以下xml
我需要获取 3 列的“总合同价值(总 NBA)”行值。我尝试使用 Document Builder 和 SAXParser,因为格式令人困惑,请帮助我修复它
<?xml version='1.0'?>
<?mso-application progid='Excel.Sheet'?>
<s:Workbook xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:s="urn:schemas-microsoft-com:office:spreadsheet">
<s:Styles>
<s:Style s:ID="REGULAR">
<s:Alignment s:WrapText="1" />
<s:Font s:FontName="Arial" s:Color="#000000" />
</s:Style>
<s:Style s:ID="BOLD">
<s:Alignment s:Vertical="Bottom" s:WrapText="1" />
<s:Font s:Bold="1" s:FontName="Arial" s:Color="#000000" />
</s:Style>
<s:Style s:ID="REGULAR_NO_WRAP">
<s:Alignment />
<s:Font s:FontName="Arial" s:Color="#000000" />
</s:Style>
<s:Style s:ID="DATE">
<s:Font s:FontName="Arial" s:Color="#000000" />
<s:NumberFormat s:Format="Short Date" />
</s:Style>
<s:Style s:ID="LINK">
<s:Alignment />
<s:Font s:Underline="Single" s:FontName="Arial" s:Color="#0000FF" />
</s:Style>
<s:Style s:ID="COLUMN_HEAD">
<s:Alignment s:Horizontal="Center" s:WrapText="1" />
<s:Font s:Bold="1" s:FontName="Arial" s:Color="#FFFFFF" />
<s:Interior s:Color="#6CA76F" s:Pattern="Solid" />
</s:Style>
<s:Style s:ID="COLUMN_SUBHEAD">
<s:Alignment s:WrapText="1" />
<s:Font s:FontName="Arial" s:Color="#006600" />
<s:Interior s:Color="#B5D6B7" s:Pattern="Solid" />
</s:Style>
<s:Style s:ID="HIGHLIGHT">
<s:Alignment s:WrapText="1" />
<s:Font s:FontName="Arial" s:Color="#000000" />
<s:Interior s:Color="#D2E3FF" s:Pattern="Solid" />
</s:Style>
<s:Style s:ID="HIGHLIGHT_BOLD">
<s:Alignment s:WrapText="1" />
<s:Font s:Bold="1" s:FontName="Arial" s:Color="#000000" />
<s:Interior s:Color="#D2E3FF" s:Pattern="Solid" />
</s:Style>
<s:Style s:ID="HIGHLIGHT_SUBTLE">
<s:Alignment s:WrapText="1" />
<s:Font s:FontName="Arial" s:Color="#000000" />
<s:Interior s:Color="#E7F0FF" s:Pattern="Solid" />
</s:Style>
<s:Style s:ID="HIGHLIGHT_LINK">
<s:Alignment s:WrapText="1" />
<s:Font s:Underline="Single" s:FontName="Arial" s:Size="14" s:Color="#000000" />
<s:Interior s:Color="#D2E3FF" s:Pattern="Solid" />
</s:Style>
<s:Style s:ID="GRANDTOTAL">
<s:Alignment s:WrapText="1" />
<s:Font s:Bold="1" s:FontName="Arial" s:Color="#000000" />
<s:Interior s:Color="#F0F0F0" s:Pattern="Solid" />
</s:Style>
<s:Style s:ID="HIGHLIGHT_C">
<s:Alignment s:WrapText="1" />
<s:Font s:FontName="Arial" s:Color="#000000" />
<s:Interior s:Color="#D2E3FF" s:Pattern="Solid" />
<s:NumberFormat s:Format="_(* #,##0.00_);_(* \(#,##0.00\);_(* 0.00_);_(@_)" />
</s:Style>
<s:Style s:ID="REGULAR_C">
<s:Alignment s:WrapText="1" />
<s:Font s:FontName="Arial" s:Color="#000000" />
<s:NumberFormat s:Format="_(* #,##0.00_);_(* \(#,##0.00\);_(* 0.00_);_(@_)" />
</s:Style>
<s:Style s:ID="HIGHLIGHT_P">
<s:Alignment s:WrapText="1" />
<s:Font s:FontName="Arial" s:Color="#000000" />
<s:Interior s:Color="#D2E3FF" s:Pattern="Solid" />
<s:NumberFormat s:Format="Percent" />
</s:Style>
<s:Style s:ID="REGULAR_P">
<s:Alignment s:WrapText="1" />
<s:Font s:FontName="Arial" s:Color="#000000" />
<s:NumberFormat s:Format="Percent" />
</s:Style>
</s:Styles>
<s:Worksheet s:Name="Service">
<s:Table>
<s:Column s:Width="170" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Column s:Width="80" />
<s:Row>
<s:Cell s:StyleID="BOLD">
<s:Data s:Type="String">Project</s:Data>
</s:Cell>
<s:Cell s:StyleID="REGULAR_NO_WRAP">
<s:Data s:Type="String">VNNWASXX-VENWAS</s:Data>
</s:Cell>
</s:Row>
<s:Row>
<s:Cell s:StyleID="BOLD">
<s:Data s:Type="String">Forecast Period</s:Data>
</s:Cell>
<s:Cell s:StyleID="REGULAR_NO_WRAP">
<s:Data s:Type="String">Oct 2015</s:Data>
</s:Cell>
</s:Row>
<s:Row>
<s:Cell s:StyleID="BOLD">
<s:Data s:Type="String">Page</s:Data>
</s:Cell>
<s:Cell s:StyleID="REGULAR_NO_WRAP">
<s:Data s:Type="String">Revenue Details</s:Data>
</s:Cell>
</s:Row>
<s:Row>
<s:Cell s:StyleID="BOLD">
<s:Data s:Type="String">Currency</s:Data>
</s:Cell>
<s:Cell s:StyleID="REGULAR_NO_WRAP">
<s:Data s:Type="String">EUR</s:Data>
</s:Cell>
</s:Row>
<s:Row>
<s:Cell s:StyleID="COLUMN_HEAD">
<s:Data s:Type="String" />
</s:Cell>
<s:Cell s:StyleID="COLUMN_HEAD">
<s:Data s:Type="String">Usa / Canada</s:Data>
</s:Cell>
<s:Cell s:StyleID="COLUMN_HEAD">
<s:Data s:Type="String">Europe / Africa</s:Data>
</s:Cell>
<s:Cell s:StyleID="COLUMN_HEAD">
<s:Data s:Type="String">Asia / Pacific</s:Data>
</s:Cell>
<s:Cell s:StyleID="COLUMN_HEAD">
<s:Data s:Type="String">EDS - EU Clinic</s:Data>
</s:Cell>
<s:Cell s:StyleID="COLUMN_HEAD">
<s:Data s:Type="String">Total</s:Data>
</s:Cell>
</s:Row>
<s:Row>
<s:Cell s:MergeAcross="5" s:StyleID="COLUMN_SUBHEAD">
<s:Data s:Type="String">Contract</s:Data>
</s:Cell>
</s:Row>
<s:Row>
<s:Cell s:StyleID="HIGHLIGHT">
<s:Data s:Type="String">Total Contract Value (Gross NBA)</s:Data>
</s:Cell>
<s:Cell s:StyleID="HIGHLIGHT_C">
<s:Data s:Type="Number">33181</s:Data>
</s:Cell>
<s:Cell s:StyleID="HIGHLIGHT_C">
<s:Data s:Type="Number">75425.67</s:Data>
</s:Cell>
<s:Cell s:StyleID="HIGHLIGHT_C">
<s:Data s:Type="Number">0</s:Data>
</s:Cell>
<s:Cell s:StyleID="HIGHLIGHT_C">
<s:Data s:Type="Number">0</s:Data>
</s:Cell>
<s:Cell s:StyleID="HIGHLIGHT_C">
<s:Data s:Type="Number">108606.67</s:Data>
</s:Cell>
</s:Row>
我尝试使用以下代码
public void readXMLFile(String fileName){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Document doc = null;
try {
doc = builder.parse(fileName);
} catch (SAXException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
NodeList labTestList = doc.getElementsByTagName("s:Worksheet");
for (int i = 0; i < labTestList.getLength(); ++i)
{
Element labTest = (Element) labTestList.item(i);
String labTestType = labTest.getAttribute("s:Name");
NodeList valueList = labTest.getElementsByTagName("s:Row");
for (int j = 0; j < valueList.getLength(); ++j)
{
Element value = (Element) valueList.item(j);
String valueType = value.getAttribute("s:Type");
NodeList conditionList = value.getElementsByTagName("s:Cell");
for (int k = 0; k < conditionList.getLength(); ++k)
{
【问题讨论】:
-
添加您尝试过的内容以及错误或令人困惑的部分
-
就目前而言,您的问题似乎并不清楚。请包含您收到的一些错误消息,可能还有一些您用于解析 XML 的 Java 代码示例。
-
我无法添加整个代码,因为它在单击保存编辑按钮时显示了一些错误,但是,我已经更新了一些部分。请看一下
-
您似乎将命名空间和标签名称混为一谈。请阅读 XML 格式。
-
没看懂,请详细说明
标签: java xml selenium selenium-webdriver