1

我正在解析以下 XML:

    <table ID="customer">
    <T><C_CUSTKEY>1</C_CUSTKEY><C_NAME>Customer#000000001</C_NAME><C_ADDRESS>IVhzIApeRb ot,c,E</C_ADDRESS><C_NATIONKEY>15</C_NATIONKEY><C_PHONE>25-989-741-2988</C_PHONE><C_ACCTBAL>711.56</C_ACCTBAL><C_MKTSEGMENT>BUILDING</C_MKTSEGMENT><C_COMMENT>regular, regular platelets are fluffily according to the even attainments. blithely iron</C_COMMENT></T>
    <T><C_CUSTKEY>2</C_CUSTKEY><C_NAME>Customer#000000002</C_NAME><C_ADDRESS>XSTf4,NCwDVaWNe6tEgvwfmRchLXak</C_ADDRESS><C_NATIONKEY>13</C_NATIONKEY><C_PHONE>23-768-687-3665</C_PHONE><C_ACCTBAL>121.65</C_ACCTBAL><C_MKTSEGMENT>AUTOMOBILE</C_MKTSEGMENT><C_COMMENT>furiously special deposits solve slyly. furiously even foxes wake alongside of the furiously ironic ideas. pending</C_COMMENT></T>
    <T><C_CUSTKEY>3</C_CUSTKEY><C_NAME>Customer#000000003</C_NAME><C_ADDRESS>MG9kdTD2WBHm</C_ADDRESS><C_NATIONKEY>1</C_NATIONKEY><C_PHONE>11-719-748-3364</C_PHONE><C_ACCTBAL>7498.12</C_ACCTBAL><C_MKTSEGMENT>AUTOMOBILE</C_MKTSEGMENT><C_COMMENT>special packages wake. slyly reg</C_COMMENT></T>
    <T><C_CUSTKEY>4</C_CUSTKEY><C_NAME>Customer#000000004</C_NAME><C_ADDRESS>XxVSJsLAGtn</C_ADDRESS><C_NATIONKEY>4</C_NATIONKEY><C_PHONE>14-128-190-5944</C_PHONE><C_ACCTBAL>2866.83</C_ACCTBAL><C_MKTSEGMENT>MACHINERY</C_MKTSEGMENT><C_COMMENT>slyly final accounts sublate carefully. slyly ironic asymptotes nod across the quickly regular pack</C_COMMENT></T>
    <T><C_CUSTKEY>5</C_CUSTKEY><C_NAME>Customer#000000005</C_NAME><C_ADDRESS>KvpyuHCplrB84WgAiGV6sYpZq7Tj</C_ADDRESS><C_NATIONKEY>3</C_NATIONKEY><C_PHONE>13-750-942-6364</C_PHONE><C_ACCTBAL>794.47</C_ACCTBAL><C_MKTSEGMENT>HOUSEHOLD</C_MKTSEGMENT><C_COMMENT>blithely final instructions haggle; stealthy sauternes nod; carefully regu</C_COMMENT></T>
</table>

使用以下java代码:

package xmlparserformining;
import java.util.List;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class XmlParserForMining {


    public static Document getDocument( final String xmlFileName )
   {
      Document document = null;
      SAXReader reader = new SAXReader();
      try
      {
         document = reader.read( xmlFileName );
      }
      catch (DocumentException e)
      {
         e.printStackTrace();
      }
      return document;
   }


    public static void main(String[] args) {



      String xmlFileName = "/home/r/javaCodez/parsing in java/customer.xml";
      String xPath = "//table/T/C_ADDRESS";
      Document document = getDocument( xmlFileName );
      List<Node> nodes = document.selectNodes( xPath );
      System.out.println(nodes.size());


        for (Node node : nodes)
      {
         String customer_address = node.valueOf(xPath);
         System.out.println( "Customer address: " + customer_address);
      }
   }

}

但是,我没有得到所有不同的客户记录,而是得到以下输出:

1500
Customer address: IVhzIApeRb ot,c,E
Customer address: IVhzIApeRb ot,c,E
Customer address: IVhzIApeRb ot,c,E
Customer address: IVhzIApeRb ot,c,E
Customer address: IVhzIApeRb ot,c,E
Customer address: IVhzIApeRb ot,c,E
Customer address: IVhzIApeRb ot,c,E
Customer address: IVhzIApeRb ot,c,E
Customer address: IVhzIApeRb ot,c,E
Customer address: IVhzIApeRb ot,c,E
Customer address: IVhzIApeRb ot,c,E
Customer address: IVhzIApeRb ot,c,E
Customer address: IVhzIApeRb ot,c,E
Customer address: IVhzIApeRb ot,c,E
Customer address: IVhzIApeRb ot,c,E
Customer address: IVhzIApeRb ot,c,E
Customer address: IVhzIApeRb ot,c,E
Customer address: IVhzIApeRb ot,c,E
Customer address: IVhzIApeRb ot,c,E
Customer address: IVhzIApeRb ot,c,E
Customer address: IVhzIApeRb ot,c,E
Customer address: IVhzIApeRb ot,c,E
Customer address: IVhzIApeRb ot,c,E
Customer address: IVhzIApeRb ot,c,E
Customer address: IVhzIApeRb ot,c,E

等等 ..

这里有什么问题?为什么只打印第一条记录?

4

1 回答 1

4

我怀疑这是问题所在:

String customer_address = node.valueOf(xPath);

您为什么要再次尝试使用 XPath 查询?你不只是想要node.getText()或类似的东西吗?

于 2012-11-28T17:02:03.063 回答