cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to read complex XML deep nested structure using Java / XSLT Mapping - SAP PI/PO

0 Kudos
944

Below is my sample source xml for Inbound ABAP Proxy Interface, and I am not able to read the value - Johnson Elementary School from below xml using my Java code or using XSLT mapping. I am able to read attributes though, but not the value passed inside tag <School> ie. Johnson Elementary School. Please suggest way to go, either Java or XSLT ?

Thanks.

Source File -

<School>

   <Student>

      <name>John</name>

       <age>15</age>

    </Student>

  JohnsonElementarySchool

</School>

Accepted Solutions (1)

Accepted Solutions (1)

sugata_bagchi2
Active Contributor
0 Kudos

Hello Amdap,
did you try the java code in the java mapping? it should work.

Thanks

Sugata

Answers (3)

Answers (3)

Muniyappan
Active Contributor
0 Kudos

https://xsltfiddle.liberty-development.net/94rmq5R. This is based on saxon 3.0

you could try similarly with xalan processor with xslt 1.0

As suggested by Anupam, better check with source team and try to send the value in the proper xml element.

anupam_ghosh2
Active Contributor
0 Kudos

Hi Amdap ps,

Please can you modify the source xml structure as shown below to include a tag <schoolName>. The reason that even after using xslt/java mapping you are facing difficulty to read the school name is the fact that the value is not coming within tags. You can try reading the school names using dom parser in java with simple statements in a nodelist. Then get individual values as per requirement.

The data structure chosen for programming greatly reduces system overhead else you need a lot of code to obtain the data therefore increasing system overload and future maintenance.

Below code snippet should help you get the school name.

DocumentBuilder parser = factory.newDocumentBuilder();

Document document = parser.parse(new File("c:\apps\payload.xml"));
NodeList schoolName = document.getElementsByTagName("schoolName");
<School>

<Student>

<name>John</name>

<age>15</age>

</Student>

<schoolName>  JohnsonElementarySchool</schoolName>

</School>

Regards
Anupam
0 Kudos

I agree with your points. But unfortunately, we cannot modify the source file.

Is there a possibility to achieve this via xslt mapping or is it completely ruled out? ( without modifying the source file).

sugata_bagchi2
Active Contributor
0 Kudos

you can use JDOM to read the xml easily, as the node contains Text value you have to treat this as mixed content xml. include JDOM2.0.6 jar in your build path. And to read the input document you can use parse xml method and input stream, I used file for testing purpose.

try to use this code to build your java map -

       SAXBuilder builder = new SAXBuilder();
            try {
                Document document = builder.build(new File("C:/test.xml"));
                Element root = document.getRootElement();
                List <Element> elems = root.getChildren();
                int i = document.getRootElement().getChildren().size();
                for (int p =0;p<i;p++)
                  { 
                    
                    List content = elems.get(p).getContent();
                    for (Object o : content) 
                {   
                    if (o instanceof Text) 
                    {
                        Text text = (Text) o;
                        String school = text.getTextTrim();
                        System.out.println(school);
                    }
                }
                 }
            }
            catch (Exception e)
            {
                e.toString();}
            }
               
            
            }    


I have used this with the xml you provided and it returned me the school name.

I have used the below xml -

<Root>
<School>

   <Student>

      <name>John</name>

       <age>15</age>

    </Student>

  JohnsonElementarySchool1

</School>
<School>

   <Student>

      <name>John</name>

       <age>15</age>

    </Student>

  JohnsonElementarySchool2

</School>

<School>

   <Student>

      <name>John</name>

       <age>15</age>

    </Student>

  JohnsonElementarySchool2666

</School>
</Root>
0 Kudos

Thanks. I will try this solution and update.