Stuck with the first record while parsing an XML in Java
Posted
by
Ritwik G
on Stack Overflow
See other posts from Stack Overflow
or by Ritwik G
Published on 2012-11-28T16:57:24Z
Indexed on
2012/11/28
17:04 UTC
Read the original article
Hit count: 279
I am parsing the following 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>
with the following java code:
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);
}
}
}
However, instead of getting all the various customer records, I am getting the following output:
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
and so on ..
What is wrong here? Why is it printing only the first record ?
© Stack Overflow or respective owner