on 02-24-2016 6:13 AM
Hi All,
I need to send total number of segments in file in one of the field of the output file.
It is file to file mapping.
Now the segments in the input file is huge so counting each of them keeping adding the values will be a tedious task.
So I am planning to write a UDF with input as count of segments and adding 10 segments in the UDF.
The UDF can be reused for counting and adding the count of next 10 segments..
The output of each UDF can again be added.
Please let me know how to achieve this.
I want to achieve something like Count Transaction Header+ count SSHID+Count SICDEI+ Count SACST........ and so on till the end, which should give me total number of segments.
Regards,
Sachi
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
i took this XML
<?xml version="1.0"?>
<class>
<student rollno="393">
<firstname>A</firstname>
<lastname>B</lastname>
<nickname>C</nickname>
<marks>D</marks>
</student>
<student rollno="493">
<firstname>E</firstname>
<lastname>F</lastname>
<nickname>G</nickname>
<marks>H</marks>
</student>
<student rollno="593">
<firstname>I</firstname>
<lastname>J</lastname>
<nickname>K</nickname>
<marks>L</marks>
</student>
<student rollno="493">
<firstname>M</firstname>
<lastname>N</lastname>
<nickname>O</nickname>
<marks>P</marks>
</student>
<student rollno="593">
<firstname>Q</firstname>
<lastname>R</lastname>
<nickname>S</nickname>
<marks>T</marks>
</student>
<studenzt>
<nickname>U</nickname>
<marks>V</marks>
</studenzt>
<nodex>
<A>
<B>ttttttt</B>
</A>
<A>dsssss</A>
</nodex>
</class>
here is the recursive java code to count only nodes no fields
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
public class parsing2
{
int counterall=1, onlynodes=1; //counter all counts all nodes only nodes counts only nodes
public static void main(String[] args)
{
(new parsing2()).ThirdParser();
}
Document d;
public void ThirdParser()
{
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
File f = new File("sample.xml");
d = builder.parse(f);
Element e = d.getDocumentElement();
if(e.hasChildNodes()){
NodeList children = e.getChildNodes();
for(int i=0;i<children.getLength();i++){
Node child = children.item(i);
visit(child);
}
System.out.println(counterall);
System.out.println(onlynodes);
}
}catch(Exception e){
e.printStackTrace();
}
}
public void visit(Node child)
{
if(child.getNodeType() == Node.ELEMENT_NODE)
{
Element e = (Element)child;
//System.out.println(child.getNodeName()+"value"+child.getTextContent()+" dsd");
if((child.getFirstChild().getNodeName()).equals("#text"))
counterall++;
if((child.getTextContent()).startsWith("\n"))
onlynodes++;
if(e.hasChildNodes())
{
NodeList list = e.getChildNodes();
for(int i=0;i<list.getLength();i++)
{
visit(list.item(i));
}
}
}
}
}
Result is 33(all elements+nodes) and 9(only nodes)
User | Count |
---|---|
86 | |
10 | |
10 | |
9 | |
6 | |
6 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.