cancel
Showing results for 
Search instead for 
Did you mean: 

How to count huge number of IDOC segments using UDF?

Former Member
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

markangelo_dihiansan
Active Contributor
0 Kudos

Hi Dubey,

Here's a little POC for you

Sample mapping

Sample input/output

The total includes the Root node. If you want to only include the children of the root node, just subtract one.

Regards,

Mark

Former Member
0 Kudos

Thanks for the reply Mark.

Can you tell me how did you get 11.0?

Hope you are not counting A,B,C,D etc.

I need to only count the segments and not the fields.

Regards,

Sachi

markangelo_dihiansan
Active Contributor
0 Kudos

Hi Dubey,

Here's an updated code:

Test

Regards,

Mark

Former Member
0 Kudos

This looks good Mark. I am trying this method.

Please let me know will it count sub-segments and sub-segments of sub-segments of SSHID??

Regards,

Sachi

Former Member
0 Kudos

Hi Mark,

I have done exactly the same what you did, but I am getting total segments as 0000. Please let me know what I am missing.

markangelo_dihiansan
Active Contributor
0 Kudos

Hi Dubey,

You forgot to include the namespace of the message type. It should start with /ns0:MT_CbsaCsa..... and not with /MT_CbsaCsa.... It will only count the segments (no subsegments) of Header, Shipment and Trailer.

Regards,

Mark

vinaymittal
Contributor
0 Kudos

Good One Mark i bookmarked it

vinaymittal
Contributor
0 Kudos

Hi,

Did you solve it yet??

Regards

Vinay

Former Member
0 Kudos

Oh ok Mark.

But I have subsegments and even sub-sub segments of shipment to be counted.

Regards,

Sachi

markangelo_dihiansan
Active Contributor
0 Kudos

Hi Dubey,

In that case, start from the root node and then subtract 4 (root node, .transactionHeader, shipment and transactionTrailer) from the total.

Regards,

Mark

vinaymittal
Contributor
0 Kudos

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)

vinaymittal
Contributor
0 Kudos

for implementing it in udf see this thread how to implement recursion in udf

vinaymittal
Contributor
0 Kudos

for taking the string xml and then making the Document you can use this code

DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();

InputSource is = new InputSource();

is.setCharacterStream(new StringReader(xmlRecords));

Document doc = db.parse(is)

markangelo_dihiansan
Active Contributor
0 Kudos

Thank you Vinay for continuing the code

vinaymittal
Contributor
0 Kudos

Actually mark i didn't knew about the "return as XML" option until i saw your screenshots.

Answers (0)