Skip to Content
avatar image
Former Member

Blanks in Java Mapping

Hello experts,

we have a 2 step mapping in our scenario. Firstly we use the graphical mapping and set blanks to all fields that are not filled.

So we have a structure like

<head></head>
<pos1>
  <value1>100</value1>
  <value2>         </value2>
</pos1>
<pos2>
  <value1>   </value1>
  <value2>         </value2>
</pos2>

When I do a java mapping after that, he ignores all blanks and makes a structure like:

<head></head>
<pos1>
  <value1>100</value1>
  <value2></value2>
</pos1>
<pos2>
  <value1></value1>
  <value2></value2>
</pos2>

How can I keep the blanks? I tried

DocumentBuilderFactory factory =
                DocumentBuilderFactory.newInstance();
            factory.setIgnoringElementContentWhitespace(false);

in the Java Code, but the blanks are still ignored...

Best regards,

David

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Best Answer
    avatar image
    Former Member
    Jul 14, 2008 at 09:54 AM

    Hi

    setIgnoringElementContentWhitespace works only if you have a DTD to validate it against.

    As regarding a solution, the one that comes to mind right away is follow the java map with another message map and once again set the whitespaces.

    BR

    Add comment
    10|10000 characters needed characters exceeded

  • Jul 14, 2008 at 09:56 AM

    Hi,

    1. Why do u require to insert a blank spaces for the fields that are not filled in the graphical mapping and then remove it in the Java mapping. Instead map a empty constant.

    2. You can only get a xml node like <value2/> and not <value2></value2>.

    If your requirement is to generate a target field always and fill it with the source value if it exists else a blank value use the following.

    Check if the source field exists(node function).

    a. If exist map the source to the target.

    b. If not map a empty constant to the target.

    Thanks

    SaNv...

    Add comment
    10|10000 characters needed characters exceeded

  • Jul 14, 2008 at 10:30 AM
    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      I chose another solution, because a third mapping was not possible due to the structure. I replaced all "> " and " <" with ">#BLANK#" and "#BLANK#<" before parsing and after mapping I replaced it back.

      I have to admit, that I'm not very lucky with this solution, but it works quite well...

      Thanks anyways,

      David

  • avatar image
    Former Member
    Jul 16, 2008 at 01:52 PM

    Can Anyone help me clearly on this Issue?

    How to Solve this?

    if any one have ..Plz provide me with Java Coding?

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hello Sathish,

      my code is:

      //Create and modify String from input
      String input_s = slurp(input);
              	
      input_s = input_s.replaceAll("> ", ">#BLANK#");
      input_s = input_s.replaceAll(" <"", "#BLANK#<");
              	
      input = new ByteArrayInputStream(input_s.getBytes());
              	
      //Create DOM parser
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = factory.newDocumentBuilder();
      
      //Parse input to create document tree
      Document doc = builder.parse(input);
      
      
      //DO YOUR MAPPING HERE
      
      
      String doc_s = doc.toString();
      doc_s = doc_s.replaceAll(">#BLANK#", "> ");
      doc_s = doc_s.replaceAll("#BLANK#<"", " <");
                  
      RESULT = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + doc_s;
      
      

      Does anybody have a better solution? It works, but I don't think, that it's the best way to do it.

      Regards,

      David

      EDIT:

      Please notice, this can lead to errors, if there are any blanks outside of tags!!!

      Edited by: David Claes on Jul 17, 2008 2:32 PM