从错误的xsl格式的文档中获取属性值

时间:2017-05-19 10:59:05

标签: java xml

我将格式错误的xsl记录为输入,从中我应该根据行和列提取一些单元格值。
我说格式化不好,因为当我用Excel打开它时,我收到警告:s
xsl文件后面的xml如下所示:

The file format and extension of ... don't match.

我设法使用以下代码获取完整节点:

<Row>
    <Cell ss:StyleID="s62">
       <Data ss:Type="String">Cell1</Data>
    </Cell>
    <Cell>
         <Data ss:Type="String">n/a</Data>
    </Cell>
</Row>

如果我打印它看起来像这样:

File fXmlFile = new File(xmlFilePath);
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("Worksheet"); // getting the tags "Worksheet"
Node nNode = nList.item(0); // getting the first "worksheet" tag
Element eElement = (Element) nNode; //getting the content of the worksheet tag
String td = eElement.getElementsByTagName("Row").item(1).getTextContent(); // item(1) gets the second "Row" tag

如何只访问第二个元素?

1 个答案:

答案 0 :(得分:3)

您应该按如下所示更新您的代码,以便获取第二行的子代,获取第二行Cell,然后获取该单元格的文本节点:

File fXmlFile = new File(xmlFilePath);
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("Worksheet"); // getting the tags "Worksheet"
Node nNode = nList.item(0); // getting the first "worksheet" tag
Element eElement = (Element) nNode; //getting the content of the worksheet tag


Element secondRow = (Element)  eElement.getElementsByTagName("Row").item(1);
Element secondCell = (Element)   secondRow.getElementsByTagName("Cell").item(1);
String secondCellText = secondCell.getTextNode();