【问题标题】:Parse xml using Java [closed]使用 Java 解析 xml [关闭]
【发布时间】: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


【解决方案1】:

您没有提到您遇到了哪些错误,这会很有帮助,但我建议您使用xpath 来导航您的 xml,一旦它是文档形式。方法 getAttribute()getElementByTagName() 可能很难从中获得可靠和有用的结果。

xpath 表达式应该是这样的:

String expression = "/s:Workbook/s:Styles/s:Worksheet";

这将为您提供包含总合约价值的节点。然后,您可以使用 lastChildNode() 并浏览单元格。

【讨论】:

  • 请提供一种方法来读取属性 Total Contract Value (Gross NBA)"
  • 我不确定这是否会像您建议的那样轻松工作 - OP 至少必须在 XPath 引擎中配置命名空间上下文。这使我们回到 OP 必须了解命名空间到底是什么以及 s:Workbook 的实际含义。
【解决方案2】:

如果你有对应的xsd,你可以生成jaxb pojo类或者用jaxb注解和命名空间编写pojo,并使用jaxb unmarshalling将整个xml映射成一个java对象。高温

【讨论】:

  • 这是为了测试目的,我需要阅读这个xml文件并与应用程序进行比较
猜你喜欢
  • 2011-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-03
  • 2011-08-22
  • 1970-01-01
  • 2011-01-08
  • 2011-07-25
相关资源
最近更新 更多