【问题标题】:How to Extract XML table data using XPath如何使用 XPath 提取 XML 表数据
【发布时间】:2013-11-11 04:55:20
【问题描述】:
<table index="1" title=" Final year marks of BIT students" ref="BIT results">
        <headings>
          <heading>Semester1</heading>
          <heading>Semester2</heading>
          <heading>Semester3</heading>
          <heading>Semester4</heading>
          <heading>Grade</heading>
          <heading>FYP</heading>
        </headings>
        <tablebody>
          <tablerow>
            <tablecell><item>10</item></tablecell>
            <tablecell><item>12</item></tablecell>
            <tablecell><item>13</item></tablecell>
            <tablecell><item>15</item></tablecell>
            <tablecell><item>B</item></tablecell>
            <tablecell><item>B</item></tablecell>
          </tablerow>
        </tablebody>
      </table>
      <table index="2" title="Final year marks of COM students" ref="COM results">
        <headings>
          <heading>Semester1</heading>
          <heading>Semester2</heading>
          <heading>Semester3</heading>
          <heading>Semester4</heading>
          <heading>Grade</heading>
          <heading>FYP</heading>
        </headings>
        <tablebody>
          <tablerow>
            <tablecell><item>15</item></tablecell>
            <tablecell><item>15</item></tablecell>
            <tablecell><item>15</item></tablecell>
            <tablecell><item>14</item></tablecell>
            <tablecell><item>A</item></tablecell>
            <tablecell><item>A</item></tablecell>
          </tablerow>
          <tablerow>
            <tablecell><item>10</item></tablecell>
            <tablecell><item>5</item></tablecell>
            <tablecell><item>9</item></tablecell>
            <tablecell><item>11</item></tablecell>
            <tablecell><item>C</item></tablecell>
            <tablecell><item>C</item></tablecell>
          </tablerow>
        </tablebody>
      </table>``

您好,我是 Xpath 的新手。我需要使用 Xpath 提取 XML 表数据。这是我的 XML 代码。我已经尝试过,但无法获得成功的输出。

The output table would be like this 

semester1  semester2  semester3 semester4  grade  fyp
 10            11          13       12         A   B

有没有人知道解决这个问题的最佳方法?谢谢你:)

【问题讨论】:

    标签: java xml xpath


    【解决方案1】:

    首先,您的示例 XML 无效。要么您提供了两个单独的示例,要么您缺少周围的父标签。

    我假设了第二个并将您的示例包装在 &lt;tables&gt;&lt;/tables&gt; 标记中。

    所以现在基于这个...

    <tables>
        <table index="1" title=" Final year marks of BIT students" ref="BIT results">
            <headings>
                <heading>Semester1</heading>
                <heading>Semester2</heading>
                <heading>Semester3</heading>
                <heading>Semester4</heading>
                <heading>Grade</heading>
                <heading>FYP</heading>
            </headings>
            <tablebody>
                <tablerow>
                    <tablecell>
                        <item>10</item>
                    </tablecell>
                    <tablecell>
                        <item>12</item>
                    </tablecell>
                    <tablecell>
                        <item>13</item>
                    </tablecell>
                    <tablecell>
                        <item>15</item>
                    </tablecell>
                    <tablecell>
                        <item>B</item>
                    </tablecell>
                    <tablecell>
                        <item>B</item>
                    </tablecell>
                </tablerow>
            </tablebody>
        </table>
        <table index="2" title="Final year marks of COM students" ref="COM results">
            <headings>
                <heading>Semester1</heading>
                <heading>Semester2</heading>
                <heading>Semester3</heading>
                <heading>Semester4</heading>
                <heading>Grade</heading>
                <heading>FYP</heading>
            </headings>
            <tablebody>
                <tablerow>
                    <tablecell>
                        <item>15</item>
                    </tablecell>
                    <tablecell>
                        <item>15</item>
                    </tablecell>
                    <tablecell>
                        <item>15</item>
                    </tablecell>
                    <tablecell>
                        <item>14</item>
                    </tablecell>
                    <tablecell>
                        <item>A</item>
                    </tablecell>
                    <tablecell>
                        <item>A</item>
                    </tablecell>
                </tablerow>
                <tablerow>
                    <tablecell>
                        <item>10</item>
                    </tablecell>
                    <tablecell>
                        <item>5</item>
                    </tablecell>
                    <tablecell>
                        <item>9</item>
                    </tablecell>
                    <tablecell>
                        <item>11</item>
                    </tablecell>
                    <tablecell>
                        <item>C</item>
                    </tablecell>
                    <tablecell>
                        <item>C</item>
                    </tablecell>
                </tablerow>
            </tablebody>
        </table>
    </tables>
    

    我可以使用...

    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathConstants;
    import javax.xml.xpath.XPathExpression;
    import javax.xml.xpath.XPathExpressionException;
    import javax.xml.xpath.XPathFactory;
    import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;
    
    public class TestXPath {
    
        public static void main(String[] args) {
            try {
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();
                Document doc = builder.parse(new File("Table.xml"));
    
                XPathFactory xFactory = XPathFactory.newInstance();
                XPath path = xFactory.newXPath();
                XPathExpression exp = path.compile("/tables/table");
                NodeList nlTables = (NodeList) exp.evaluate(doc, XPathConstants.NODESET);
                for (int tblIndex = 0; tblIndex < nlTables.getLength(); tblIndex++) {
    
                    Node table = nlTables.item(tblIndex);
                    Node nAtt = table.getAttributes().getNamedItem("title");
                    System.out.println("Showing results for " + (nAtt == null ? "[Unknown]" : nAtt.getTextContent()));
    
                    exp = path.compile("headings/heading");
                    NodeList nlHeaders = (NodeList) exp.evaluate(table, XPathConstants.NODESET);
                    Set<String> headers = new HashSet<String>(25);
                    for (int index = 0; index < nlHeaders.getLength(); index++) {
                        headers.add(nlHeaders.item(index).getTextContent().trim());
                    }
    
                    for (String header : headers) {
                        System.out.printf("%-20s", header);
                    }
                    System.out.println("");
    
                    exp = path.compile("tablebody/tablerow");
                    NodeList nlRows = (NodeList) exp.evaluate(table, XPathConstants.NODESET);
                    for (int index = 0; index < nlRows.getLength(); index++) {
                        Node rowNode = nlRows.item(index);
                        exp = path.compile("tablecell/item");
                        NodeList nlValues = (NodeList) exp.evaluate(rowNode, XPathConstants.NODESET);
                        List<String> values = new ArrayList<String>(25);
                        for (int valueIndex = 0; valueIndex < nlValues.getLength(); valueIndex++) {
                            values.add(nlValues.item(valueIndex).getTextContent().trim());
                        }
                        for (String value : values) {
                            System.out.printf("%-20s", value);
                        }
                        System.out.println("");
                    }
                    System.out.println("");
                }
    
            } catch (ParserConfigurationException exp) {
                exp.printStackTrace();
            } catch (SAXException | IOException | XPathExpressionException ex) {
                ex.printStackTrace();
            }
        }
    
    }
    

    生产...

    Showing results for  Final year marks of BIT students
    Semester1           Semester2           Semester3           Semester4           FYP                 Grade               
    10                  12                  13                  15                  B                   B                   
    
    Showing results for Final year marks of COM students
    Semester1           Semester2           Semester3           Semester4           FYP                 Grade               
    15                  15                  15                  14                  A                   A                   
    10                  5                   9                   11                  C                   C   
    

    基本上,您需要能够将每个部分分解为可管理的部分...

    查看XPath Tutorial 并阅读关于 SO 的所有关于 XPath 的帖子;)

    【讨论】:

    • 非常感谢您对我的程序的反馈。我会对此进行测试,然后告诉你它是如何进行的。问候。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    相关资源
    最近更新 更多