Skip to Content
avatar image
Former Member

Java Mapping using DOM

Hi All,

I need Java mapping help. I will get incomming payload as following

<orders>

<matnr>123</matnr>

<qty>10</qty>

</orders>

i need to change the above payload as follows

<ns0:orders>

<ns0:matnr>123</ns0:matnr>

<ns0:qty>10</ns0:qty>

</ns0:orders>

Please help me how to achieve this using Java mapping.

Regards

Vijay

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • avatar image
    Former Member
    Mar 13, 2008 at 06:49 AM

    Hi,

    Dom will be quite expensive for this type of requirement since you will have to go to each element and then append the namespace.

    I suggest you use SAX parser which is more efficient for such requirement.

    The code is given below you can modify it according to your requirement.

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    
    import javax.xml.parsers.FactoryConfigurationError;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    
    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
    
    
    public class SAXDemo extends DefaultHandler{
    	public StringBuffer sb = new StringBuffer();
    	public String PREFIX = "ns0:";
    	int count = 1;
    	public static void main(String[] args) 
    	{
    		SAXDemo demo= new SAXDemo();
    		demo.saxdemo();
    		System.out.println();
    	}
    	public void saxdemo()
    	{
    		InputStream in;
    		try 
    		{
    			in = new FileInputStream(new File("Input.xml"));
    		
    			SAXParserFactory factory = SAXParserFactory.newInstance();
    
    			factory.setNamespaceAware(true);
    			factory.setValidating(false);
    		
    							
    			SAXParser saxParser = factory.newSAXParser();
    			
    			
    			/**
    			* Parse the content of the given {@link java.io.InputStream}
    			* instance as XML using the specified
    			* {@link org.xml.sax.helpers.DefaultHandler}.
    			*
    			* @param inputstream InputStream containing the content to be parsed.
    			* @param cobject The SAX DefaultHandler to use.
    			* @exception IOException If any IO errors occur.
    			* @exception IllegalArgumentException If the given InputStream is null.
    			* @exception SAXException If the underlying parser throws a
    			* SAXException while parsing.			
    			*/
    			saxParser.parse(in, this);
    			System.out.println(sb.toString());
    		} 
    		catch (FactoryConfigurationError e) 
    		{
    			e.printStackTrace();
    		} 
    		catch (ParserConfigurationException e) 
    		{
    			e.printStackTrace();
    		} 
    		catch (SAXException e) 
    		{
    			e.printStackTrace();
    		} 
    		catch (IOException e) 
    		{
    			e.printStackTrace();
    		}
    		
    	}
    	
    	
    	public void startDocument()throws SAXException
    	{
    		
    	}
    	public void endDocument()throws SAXException
    	{}
    	public void startElement(String namespaceURI, String name, String qName, Attributes attrs)
    	throws SAXException 
    	{
    		if(count == 1)
    		{
    			sb.append("<"+PREFIX+qName+" "+PREFIX+"xmlns="+namespaceURI+">");
    		}
    		else
    		{
    			sb.append("<"+name+">");
    		}
    		count ++;
    	}
    	public void endElement(String uri, String name, String qName) throws SAXException 
    	{
    		sb.append("</"+qName+">");
    	}
    	public void characters(char buf[], int offset, int len)
    	throws SAXException 
    	{
    	
    		String s = new String(buf, offset, len);
    		
    		buf  = null;
    		
    		sb.append(s.trim());
    			s = null;
    	}
    
    }
    

    Regards

    Fariha

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Mar 13, 2008 at 09:25 AM

    Hi,

    My problem resolved using String manipulations. Thanks for the code.

    Regards

    Vijayanand Poreddy

    Add comment
    10|10000 characters needed characters exceeded