• Post Reply Bookmark Topic Watch Topic
  • New Topic

JRE 1.4.2 to 1.5 Migration issue in XML  RSS feed

 
Varatharajan Irusappan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am facing a problem in compatibility between JRE 1.4.2 and JRE1.5.0
Hope someone can help me in solving this issue.

I have an XML as described below.

<test>
<LogType type="string">Audit</LogType>
<maxMOCount type="uint32">30</maxMOCount>
<LogSize type="uint32">10489</LogSize>
<test type="string">abc defghijklmnopqrstuvwxyz</test>
</test>
Note:
=====
1.There are two test elements one containing the other.
2.The value of the second test element contains two whitespaces in it.


While using JRE1.4.2 the value of the internal test element is returning properly (with two whitespaces in between) with or without the crimson jar in classpath (since JRE 1.4.2 uses crimson jar by default)
So the output is abc<blank space1><blank space2>defghijklmnopqrstuvwxyz.

While using JRE 1.5.0 the value of the internal test element is returning null without the crimson jar in classpath and more surprisingly when the crimson jar is in classpath one of the two whitespaces vanishes.
Now the output is abc<blank space1>defghijklmnopqrstuvwxyz.

Has anyone faced similar issue while migrating from 1.4.2 to 1.5.0


Regards,
Varath
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

I suspect that you'll need to tell us a bit more about how you're accessing your XML file -- i.e., show us a little code.
 
Varatharajan Irusappan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi here is the code snippet that you can use to reproduce the issue I said.
Herewith I have attached two class files XMLGetter.java and TestCursor.java

1) XMLGetter.java
==================

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Logger;

/**
* Created by IntelliJ IDEA.
* User: ivaratharajan
* Date: Mar 29, 2006
* Time: 10:27:39 AM
* To change this template use File | Settings | File Templates.
*/
public class XMLGetter
{
/**
* $File: //swdepot/main/src_nm/javasrc/com/infinera/nm/common/utils/xml/XMLGetter.java $
* $Revision: #25 $ $Change: 117736 $ $DateTime: 2005/08/16 01:19:47 $
* $Author: kpydi $
* Copyright (c) 2002,2003 Infinera.
* All rights reserved.

* This software is the confidential and proprietary information of Infinera
* ("Confidential Information"). You shall not disclose such Confidential
* Information and shall use it only in Accordance with the terms of the
* License agreement you entered into with Infinera.
*
*
* @since 1.0
*/

// package com.infinera.nm.common.utils.xml;

private Logger _logger = Logger.getLogger(this.getClass().getName());

private Document doc;
private Element root;
private NodeList children;
private int lenOfChild = 0;
private Element firstElem;
private NodeList paramList;
private int paramListLen = 0;

/**
* Creates a new <code>XMLGetter</code> instance.
*
* @param rootNode a <code>Node</code> value, rootNode of the Document with only one Children
*/
public XMLGetter(Node rootNode)
{
root = (Element)rootNode;
if(root != null)
{
firstElem = root;
if (firstElem != null)
{
paramList = firstElem.getChildNodes();
paramListLen = paramList.getLength();
}
}
}


/**
* Creates a new <code>XMLGetter</code> instance.
*
* @param doc1 a <code>Document</code> value
*/
public XMLGetter(Document doc1)
{
doc = doc1;
doGeneralStuff();
}

/**
* <code>getDocument</code> method returns the document
* object with which it was initialized.
* Caller of this function should make sure this function will be called
* only after the instance is Initialized and also make sure that return
* Value is not Null before using it.
*
* @return a <code>Document</code> value
*/
public Document getDocument()
{
//_logger.finest("Entering ....");
if (doc == null)
{
_logger.finer("Document is null");
}
return doc;
}

/**
* <code>setDocument</code> method sets the Document object.
*
* @param doc1 a <code>Document</code> value
*/
public void setDocument(Document doc1)
{
//_logger.finest("Entering ....");
doc = doc1;
}

/**
* <code>doGeneralStuff</code> method Initializes the required stuff.
*
*/
public void doGeneralStuff()
{
//_logger.finest("Entering ....");
doGeneralStuff("test");
}

/**
* <code>doGeneralStuff</code> method Initializes the required stuff.
*
*/
public void doGeneralStuff(String seperatorTag)
{
//_logger.finest("Entering ....");
if(doc != null)
{
root = doc.getDocumentElement();
System.out.println("The Root element is " + root);
if(root != null)
{
root.normalize();
children = root.getElementsByTagName(seperatorTag);
lenOfChild = children.getLength();
if(children != null)
{
firstElem = (Element) children.item(0);
System.out.println("===> Child is " + firstElem);
if (firstElem != null)
{
paramList = firstElem.getChildNodes();
paramListLen = paramList.getLength();
}
}
}
}
}

/**
* Describe <code>toPrint</code> method here.
*
*/
public void toPrint()
{
//_logger.finest("Entering ....");
String retValue = null;

Node nextNode = children.item(0);
Element nextMessage = (Element)nextNode;

for(int i = 0; i < nextMessage.getChildNodes().getLength(); i++)
{
if(nextMessage.getChildNodes().item(i) != null)
{
_logger.finest(nextMessage.getChildNodes().item(i).toString());
}
retValue = nextMessage.getFirstChild().getNodeValue();
_logger.finest("Return Value is " + retValue);
}
}


/**
* <code>getMOIdList</code> method returns the Vector of MoIds.
* for example if the attrTag is "ContainedObjList" and the document
* looks like below will return Vector with
* the "[/SUNNYVALE/CHASSIS=1, /SUNNYVALE/CHASSIS=2]"
*
* <ContainedObjList type="List">
* <ListItem>
* <MoId type="string">/SUNNYVALE/CHASSIS=1</MoId>
* </ListItem>
* <ListItem>
* <MoId type="string">/SUNNYVALE/CHASSIS=2</MoId>
* </ListItem>
* </ContainedObjList>
*
*
* @param attrTag a <code>String</code> value
* @return a <code>Vector</code> value
*/
public Vector getListValues(String attrTag,String seperator)
{
//_logger.finest("Entering ....");
//_logger.finest(attrTag);
Vector retVector = null;

for (int j = 0 ; j < paramListLen ; j++)// walk the param list
{
Node itemNode = paramList.item(j);
// System.out.println("Test from Getter 11" + itemNode);

if(itemNode != null &&
(itemNode.getNodeName()).equals(attrTag))
{
//stem.out.println("Test from Getter 56655: " + itemNode.getChildNodes().getLength());
NodeList listItemList = itemNode.getChildNodes();

if(listItemList != null)
{
int listItenLen = listItemList.getLength();
for(int k = 0; k < listItenLen; k++)
{
Node nn1 = listItemList.item(k);
if (nn1.getNodeType() == Node.ELEMENT_NODE)
{
String tempMoId = getAttrValue(nn1,seperator);

if(retVector == null)
{
// Create Vector only when it finds a non-null String
if(tempMoId != null)
{
retVector = new Vector();
}
}
// Add only if the String is Not - null
if(tempMoId != null)
{
retVector.add((Object)tempMoId);
}
}
}
}
break;
}
}
return retVector;
}

/**
* <code>getArrayListValues</code> method returns the ArrayList of MoIds.
* for example if the attrTag is "ContainedObjList" and the document
* looks like below will return ArrayList with
* the "[/SUNNYVALE/CHASSIS=1, /SUNNYVALE/CHASSIS=2]"
*
* <ContainedObjList type="List">
* <ListItem>
* <MoId type="string">/SUNNYVALE/CHASSIS=1</MoId>
* </ListItem>
* <ListItem>
* <MoId type="string">/SUNNYVALE/CHASSIS=2</MoId>
* </ListItem>
* </ContainedObjList>
*
*
* @param attrTag a <code>String</code> value
* @return a <code>ArrayList</code> value
*/
public ArrayList getArrayListValues(String attrTag,String seperator)
{
//_logger.finest("Entering ....");
//_logger.finest(attrTag);
ArrayList retArrayList = null;

for (int j = 0 ; j < paramListLen ; j++)// walk the param list
{
Node itemNode = paramList.item(j);
// System.out.println("Test from Getter 11" + itemNode);

if(itemNode != null &&
(itemNode.getNodeName()).equals(attrTag))
{
//stem.out.println("Test from Getter 56655: " + itemNode.getChildNodes().getLength());
NodeList listItemList = itemNode.getChildNodes();

if(listItemList != null)
{
int listItenLen = listItemList.getLength();
for(int k = 0; k < listItenLen; k++)
{
Node nn1 = listItemList.item(k);
if (nn1.getNodeType() == Node.ELEMENT_NODE)
{
String tempMoId = getAttrValue(nn1,seperator);

if(retArrayList == null)
{
// Create Vector only when it finds a non-null String
if(tempMoId != null)
{
retArrayList = new ArrayList();
}
}
// Add only if the String is Not - null
if(tempMoId != null)
{
retArrayList.add((Object)tempMoId);
}
}
}
}
break;
}
}
return retArrayList;
}

/**
* <code>getMOIdList</code> method returns the Vector of MoIds.
* for example if the attrTag is "ContainedObjList" and the document
* looks like below will return Vector with
* the "[/SUNNYVALE/CHASSIS=1, /SUNNYVALE/CHASSIS=2]"
*
* <ContainedObjList type="List">
* <ListItem>
* <MoId type="string">/SUNNYVALE/CHASSIS=1</MoId>
* </ListItem>
* <ListItem>
* <MoId type="string">/SUNNYVALE/CHASSIS=2</MoId>
* </ListItem>
* </ContainedObjList>
*
*
* @param attrTag a <code>String</code> value
* @return a <code>Vector</code> value
*/
public ArrayList getListValuesArray(String attrTag,String seperator)
{
//_logger.finest("Entering ....");
//_logger.finest(attrTag);
ArrayList retArrayList = null;

for (int j = 0 ; j < paramListLen ; j++)// walk the param list
{
Node itemNode = paramList.item(j);
// System.out.println("Test from Getter 11" + itemNode);

if(itemNode != null &&
(itemNode.getNodeName()).equals(attrTag))
{
//stem.out.println("Test from Getter 56655: " + itemNode.getChildNodes().getLength());
NodeList listItemList = itemNode.getChildNodes();

if(listItemList != null)
{
int listItenLen = listItemList.getLength();
for(int k = 0; k < listItenLen; k++)
{
Node nn1 = listItemList.item(k);
if (nn1.getNodeType() == Node.ELEMENT_NODE)
{
String tempMoId = getAttrValue(nn1,seperator);

if(retArrayList == null)
{
// Create Vector only when it finds a non-null String
if(tempMoId != null)
{
retArrayList = new ArrayList();
}
}
// Add only if the String is Not - null
if(tempMoId != null)
{
retArrayList.add((Object)tempMoId);
}
}
}
}
break;
}
}
return retArrayList;
}

/**
* <code>getAttrValue</code> method returns the value for a
* Given Attribute Tag inside a given ItemListNode.
*
* @param ItemListNode a <code>Node</code> value
* @param attrTag a <code>String</code> value
* @return a <code>String</code> value
*/
public String getAttrValue(Node ItemListNode,
String attrTag)
{
// System.out.println("Test from Getter 61");
//_logger.finest(attrTag);
String retValue = null;
if(ItemListNode != null)
{
NodeList nn2 = ItemListNode.getChildNodes();
if(nn2 != null)
{
int len = nn2.getLength();
for (int j = 0 ; j < len ; j++)// walk the Node list
{
Node itemNode = nn2.item(j);
if (itemNode != null &&
itemNode.getNodeType() == Node.ELEMENT_NODE &&
((itemNode.getNodeName()).equals(attrTag)))
{
Text node = (Text)itemNode.getFirstChild();
if(node != null)
{
retValue = node.getData();
break;
}
}
}
}
}
return retValue;
}

/**
* <code>getAttrValue</code> method returns the Value of
* a Given AttrbuteTag.
*
* @param attrTag a <code>String</code> value
* @return a <code>String</code> value
*/
public String getAttrValue(String attrTag)
{
//_logger.finest("Entering ....");
//_logger.finest(attrTag);
String retValue = null;

for (int j = 0 ; j < paramListLen ; j++)// walk the param list
{
Node itemNode = paramList.item(j);

if (itemNode != null &&
itemNode.getNodeType() == Node.ELEMENT_NODE &&
((itemNode.getNodeName()).equals(attrTag)))
{
Text node = (Text)itemNode.getFirstChild();
if(node != null)
{
retValue = node.getData();
break;
}
}
}
return retValue;
}

/**
* <code>getRoot</code> method here.
*
* @return an <code>Element</code> value
*/
public Element getRoot()
{
//_logger.finest("Entering ....");
return root;
}

/* End of XMLGetter.java */

}

2)TestCursor.java
==================

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
public class TestCursor
{

public static Document getCursorRequestDoc(Document respDoc) //throws IErrorException
{
//_logger.finest("Entering .......");
try
{
// Getting cursor elements
NodeList nl =
respDoc.getDocumentElement().getElementsByTagName("test");

int nlsize = nl.getLength();
Document cursorDoc = null;

if (nlsize != 0)
{
DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = fac.newDocumentBuilder();
cursorDoc = docBuilder.newDocument();
Element cursorElem = (Element) nl.item(0);
Node impNode = cursorDoc.importNode(cursorElem, true);
cursorDoc.appendChild(impNode);
}
return cursorDoc;
}
catch (Exception pce)
{
pce.printStackTrace();
}
return null;
}

public static void main(String[] args)
{
try
{
// Get a JAXP parser factory object
// javax.xml.parsers.DocumentBuilderFactory dbf =
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
// Tell the factory what kind of parser we want
dbf.setValidating(false);
// Use the factory to get a JAXP parser object
// javax.xml.parsers.DocumentBuilder parser = dbf.newDocumentBuilder();
DocumentBuilder parser = dbf.newDocumentBuilder();

// Tell the parser how to handle errors. Note that in the JAXP API,
// DOM parsers rely on the SAX API for error handling
parser.setErrorHandler(new org.xml.sax.ErrorHandler()
{
public void warning(SAXParseException e)
{
System.err.println("WARNING: " + e.getMessage());
}
public void error(SAXParseException e)
{
System.err.println("ERROR: " + e.getMessage());
}
public void fatalError(SAXParseException e)
throws SAXException
{
System.err.println("FATAL: " + e.getMessage());
throw e; // re-throw the error
}
});

// Finally, use the JAXP parser to parse the file. This call returns
// A Document object. Now that we have this object, the rest of this
// class uses the DOM API to work with it; JAXP is no longer required.
Document document = parser.parse("logPacket.xml");
// ByteArrayOutputStream baO = new ByteArrayOutputStream(1560); // 1.5K bytes
// baO.reset();
// Transformer trans = TransformerFactory.newInstance().newTransformer();
Document doc = getCursorRequestDoc(document);
/*DOMSource domSource = new DOMSource(doc.getDocumentElement());
StreamResult streamResult = new StreamResult(baO);
trans.setOutputProperty(OutputKeys.INDENT, "yes");
trans.transform(domSource, streamResult);
byte[] data = baO.toByteArray();
StringBuffer buf = new StringBuffer();
for( int i=0; i < data.length; i++)
{
buf.append((char) data[i]);
}*/

// System.out.println(buf.toString());

XMLGetter xmlGetter = new XMLGetter(doc);

System.out.println(xmlGetter.getRoot().getElementsByTagName("test").item(0));//.getTextContent());
}
catch(Exception pce)
{
pce.printStackTrace();
}

}
}
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!