3 Replies Latest reply: Dec 14, 2007 11:10 AM by 807603 RSS

    Passing XML as Parameters to JNI

    807603
      Hi! All. I am new to JNI programming. We have a small problem. The Number of arguments and type of arguments that has to be passed to my JNI keeps changing time and again. So I am being suggested to send an xml as a string parameter, so that every time I call this JNI I have to just change the xml. But I have absolutely no idea how to do it, can somebody please suggest me how to do it. Any sample code would be greatly appreciated.

      Thanks & Regards
        • 1. Re: Passing XML as Parameters to JNI
          807603
          XML MIGHT BE overkill. (Maybe not - the XML libraries are getting easier and easier. Also: you could implement a subset of XML and probably solve your problem.)

          Having done this once myself I suggest you look at the problem like this:

          1. The objective is to pass your data across the JNI interface as a string. I will asume that you can figure out how to do this.

          2. But the data you have to pass is more complicated. What you need to do is to define it as a set of name = value pairs, then reduce this to an XML string and pass it across the interface. Your data might be even more complicated, requiring - say - something like a record structure with fields and subfields. In that case, XML will also work, because XLM fundamentally describes list of lists (of lists). So each record can be a list of name/value pairs, and the values can also be lists.

          3. Java provides the XML DOM object, which can be instantiated from an XML string. It also has "insert" methods, so you can stuff things into a DOM. there is bound to be a silar library for C/C++ which can be used on the other side of the JNI interface.


          4. An alternative is to write your own DOM parser that handles only a subset of XML. There are several of these that people have already done out there on the web. Just google for it. You might find one you like already in JAVA, and reimplement it in C. or vice-versa.

          5. Finally, an alternative might be to define an XML-equivalent that just uses - say - brackets instead of tags. A structure might look like
          {type=record, field1 = {subfield1=xxx, subfield2=yyy}}

          A friend wrote a parser for this kind of structure in less than a page of code.
          • 2. Re: Passing XML as Parameters to JNI
            807603
            bschauwejava wrote:
            XML MIGHT BE overkill. (Maybe not - the XML libraries are getting easier and easier. Also: you could implement a subset of XML and probably solve your problem.)
            4. An alternative is to write your own DOM parser that handles only a subset of XML. There are several of these that people have already done out there on the web. Just google for it. You might find one you like already in JAVA, and reimplement it in C. or vice-versa.

            5. Finally, an alternative might be to define an XML-equivalent that just uses - say - brackets instead of tags. A structure might look like
            {type=record, field1 = {subfield1=xxx, subfield2=yyy}}

            A friend wrote a parser for this kind of structure in less than a page of code.
            In my opinion, writing your own xml parser or * shudder * inventing your own version of xml and writing a parser for that is definitely overkill. There are plenty of options for xml parsers in java and c++, many of which are very easy to use. I see no benefit in redoing that work, only extra work for yourself. Just pass the string, and parse it with an xml library on the other side. It's easy to stick the params in a map, and you're done.

            A snag I can foresee is with the character encoding. Make sure that you send the string through jni with the proper character encoding so it can be decoded on the other side.
            • 3. Re: Passing XML as Parameters to JNI
              807603
              Well Hunter9000, I might agree with you, depending on a) the java, and b) the C++, and c) the experience of OP.

              o The java DOM stuff is pretty good these days - built into java, and documented.
              o I'm less sure about C++. If I had to use open source libraries and track down prequisites, before my eyes glazed over I would opt for inventing the dumbed-down language and writing the parser.