0

My xml looks like which I created using Java JAXBContext and Marshaller. I want to format some part of xml only not the whole xml.

<?xml version="1.0" encoding="UTF-8"?>
<ns4:Requests xmlns:ns2="http://www.dummy.com/xsd/tublu/murmur_001" xmlns:ns3="http://www.dummy.com/xsd/CommonObjects_001" xmlns:ns4="http://www.dummy.com/xsd/naku_001">
   <ns4:RequestSetId>fhskgvseruigiu</ns4:RequestSetId>
   <ns4:RequestStream>CHAPP</ns4:RequestStream>
   <ns4:Request>
      <ns4:TrackAndTrace>
         <ns4:CPAId>003</ns4:CPAId>
         <ns4:CorrelationId>ytuty</ns4:CorrelationId>
      </ns4:TrackAndTrace>
   </ns4:Request>
   <ns4:Request>
      <ns4:TrackAndTrace>
         <ns4:CPAId>003</ns4:CPAId>
         <ns4:CorrelationId>cyuri7</ns4:CorrelationId>
      </ns4:TrackAndTrace>
   </ns4:Request>
</ns4:Requests>

I want to format like

<?xml version="1.0" encoding="UTF-8"?>
<ns4:Requests xmlns:ns2="http://www.dummy.com/xsd/tublu/murmur_001" xmlns:ns4="http://www.dummy.com/xsd/naku_001" xmlns:ns3="http://www.dummy.com/xsd/CommonObjects_001">
    <ns4:RequestSetId>fhskgvseruigiu</ns4:RequestSetId>
    <ns4:RequestStream>CHAPP</ns4:RequestStream>
    <ns4:Request xmlns:ns4="http://www.dummy.com/xsd/naku_001"><ns4:TrackAndTrace><ns4:CPAId>003</ns4:CPAId><ns4:CorrelationId>ytuty</ns4:CorrelationId></ns4:TrackAndTrace></ns4:Request>
    <ns4:Request xmlns:ns4="http://www.dummy.com/xsd/naku_001"><ns4:TrackAndTrace><ns4:CPAId>003</ns4:CPAId><ns4:CorrelationId>cyuri7</ns4:CorrelationId></ns4:TrackAndTrace></ns4:Request>
</ns4:Requests>
2
  • What do you mean by "I want to format some part of xml". I guess, you want the single line formatting for every node tree. Right? Commented Sep 24, 2019 at 10:26
  • Yes, I want to linearized a complete element which is "Request" in my case. Commented Sep 24, 2019 at 10:38

1 Answer 1

1

Here is the solution (by Transforming the XML Data using Java's XSLT APIs),

As you may also have noticed.. JAXB alone cannot meet this requirement, but after marshalling the object to a formatted XML String (as u have shown) you can then post process/transform it accordingly using a suitable XSLT file

So to get a linearized 'Request' element, just make use of the xsl shown below:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:strip-space elements="TrackAndTrace"/> 
    <xsl:strip-space elements="Request"/> 

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

Note: Also tested that above method/approach is working properly - used the Stylizer sample code (from https://docs.oracle.com/javase/tutorial/jaxp/xslt/transformingXML.html)

Cheers!

Update: If you want a solution that also preserves the original namespace prefix as shown in your question, follow this variation Add factory.setNamespaceAware(true); in the Stylizer code & Use this tweaked XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="w3.org/1999/XSL/Transform" xmlns:ns4="dummy.com/xsd/naku_001">
<xsl:strip-space elements="ns4:TrackAndTrace"/>
<xsl:strip-space elements="ns4:Request"/> 
   <xsl:template match="@*|node()">
       <xsl:copy>
           <xsl:apply-templates select="@*|node()"/>
       </xsl:copy>
   </xsl:template>
</xsl:stylesheet> 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.