Print prefix early in Xrces-J

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Philipp

    Print prefix early in Xrces-J

    Hello,
    I have a DOM which serializes to the following XML. As you see, the
    namespace prefixed with "f" is repeated for each object. Is there a way
    in Xerces-J to make an unique definition of the prefix in the top-level
    element (so as to save room)?

    Thanks for answers
    Phil

    <?xml version="1.0" encoding="UTF-8"?>
    <e:test xmlns:e="http://www.example.com/e">
    <f:value xmlns:f="http://www.example.com/f">
    <f:val>12</f:val>
    <f:unit>lux</f:unit>
    </f:value>
    <f:value xmlns:f="http://www.example.com/f">
    <f:val>34.123 4</f:val>
    <f:unit>amper e</f:unit>
    </f:value>
    <f:value xmlns:f="http://www.example.com/f">
    <f:val>45.0</f:val>
    <f:unit>empty_k ey</f:unit>
    </f:value>
    </e:test>
  • Martin Honnen

    #2
    Re: Print prefix early in Xrces-J

    Philipp wrote:
    I have a DOM which serializes to the following XML. As you see, the
    namespace prefixed with "f" is repeated for each object. Is there a way
    in Xerces-J to make an unique definition of the prefix in the top-level
    element (so as to save room)?
    It might work to create an xmlns:f attribute on the root element e.g.

    documentInstanc e.getDocumentEl ement().setAttr ibuteNS("http://www.w3.org/2000/xmlns/",
    "xmlns:f", "http://www.example.com/f");

    That way the serialization code is hopefully smart enough to avoid
    duplicating the namespace declaration on the child elements.




    --

    Martin Honnen

    Comment

    • Philipp

      #3
      Re: Print prefix early in Xrces-J

      Martin Honnen wrote:
      Philipp wrote:
      >
      >I have a DOM which serializes to the following XML. As you see, the
      >namespace prefixed with "f" is repeated for each object. Is there a
      >way in Xerces-J to make an unique definition of the prefix in the
      >top-level element (so as to save room)?
      >
      It might work to create an xmlns:f attribute on the root element e.g.
      >
      documentInstanc e.getDocumentEl ement().setAttr ibuteNS("http://www.w3.org/2000/xmlns/",
      "xmlns:f", "http://www.example.com/f");
      >
      That way the serialization code is hopefully smart enough to avoid
      duplicating the namespace declaration on the child elements.
      I tried it and unfortunately it's not smart enough :-( It really
      duplicates the namespace on each child...

      Comment

      • Martin Honnen

        #4
        Re: Print prefix early in Xrces-J

        Philipp wrote:
        I tried it and unfortunately it's not smart enough :-( It really
        duplicates the namespace on each child...
        Are you sure those f:value elements do not have an xmlns:f attribute on
        their own? If they have then I don't think the serializer will omit them.
        So doing
        NodeList values =
        documentInstanc e.getElementsBy TagName("http://www.example.com/f", "value");
        for (int i = 0, l = values.getLengt h(); i < l; i++)
        {
        Element value = (Element)values .getItem(i);
        value.removeAtt ributeNS("http://www.w3.org/2000/xmlns/", "f");
        }
        might be necessary, depending on how your DOM looks.

        --

        Martin Honnen

        Comment

        • Philipp

          #5
          Re: Print prefix early in Xrces-J

          Martin Honnen wrote:
          Philipp wrote:
          >
          >I tried it and unfortunately it's not smart enough :-( It really
          >duplicates the namespace on each child...
          >
          Are you sure those f:value elements do not have an xmlns:f attribute on
          their own? If they have then I don't think the serializer will omit them.
          I typically create them with:
          Element node = document.create ElementNS("http ://www.example.com/f",
          "f:value");
          root.appendChil d(node);

          This automatically creates the xmlns:f attributes in the output.
          So doing
          NodeList values =
          documentInstanc e.getElementsBy TagName("http://www.example.com/f", "value");
          for (int i = 0, l = values.getLengt h(); i < l; i++)
          {
          Element value = (Element)values .getItem(i);
          value.removeAtt ributeNS("http://www.w3.org/2000/xmlns/", "f");
          }
          might be necessary, depending on how your DOM looks.
          Yes, I will try that...

          Comment

          Working...