在Java中解析XML时,坚持使用第一条记录

时间:2012-11-28 16:57:25

标签: java xml-parsing dom4j

我正在解析以下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

依旧......

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

1 个答案:

答案 0 :(得分:4)

我怀疑这是问题所在:

String customer_address = node.valueOf(xPath);

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