1

我遇到了问题,其中 XML 格式在名字中包含日文字符,它被转换为 ByteArrayInputStream,然后该数组被传递给 javax.xml.transform.stream 的 StreamSource,我们正在丢失 unicode 字符(日文字符) 并替换为 ????, 我们怀疑变换方法和遍历方法。请帮助我们如何取回原始的 unicode 字符或如何将其转换为字符串。或向我们建议替代解决方案。

我们使用的是 java 1.6 和 tomcat 7。

我正在使用以下代码,

String sNVPairs = "<XMLMessage><identifier>111</identifier><RegnID>10491615</RegnID><Client_Name>Binga</Client_Name><Candidate_FName>ガネーシュ&lt;/Candidate_FName><Candidate_LName>D</Candidate_LName><Candidate_ID>99296085599</Candidate_ID><Home_Zip>91505</Home_Zip><Date_Of_Request>01/20/2017</Date_Of_Request><Email_From>support@binga.com</Email_From><Email_To>Ganesh.Bhagavath@harman.com</Email_To><RA> 20 INCH TABLE TOP EXTENDER, DOUBLE TIME</RA></XMLMessage>";

String webServerStr = "http://localhost:70/email/AccomodationReuestEmail.xsl";

ByteArrayInputStream in = new ByteArrayInputStream(sNVPairs.getBytes());

javax.xml.transform.Source xmlSource = new javax.xml.transform.stream.StreamSource(in);

javax.xml.transform.Source xsltSource =new javax.xml.transform.stream.StreamSource(webServerStr);

javax.xml.transform.Result result = new javax.xml.transform.dom.DOMResult();

javax.xml.transform.TransformerFactory transFact =
              javax.xml.transform.TransformerFactory.newInstance( );

javax.xml.transform.Transformer trans;

try {
        trans = transFact.newTransformer(xsltSource);
        trans.transform(xmlSource, result);

        EMailParser msgFinder = new EMailParser();
        TreeWalker tw = new TreeWalker(msgFinder);
        tw.traverse(((DOMResult)result).getNode());
}
catch (TransformerConfigurationException e) {
        System.out.println(e.getMessage());
    } catch (TransformerException e) {
        System.out.println(e.getMessage());
    }

这是 EMailParser 代码,

public class EMailParser extends org.xml.sax.HandlerBase{
//boolean variables which stores the flag corresponding the element
private boolean m_bFrom = false;
private boolean m_bTo   = false;
private boolean m_bCc   = false;
private boolean m_bBCc   = false;
private boolean m_bBody = false;
private boolean m_bSub  = false;

//string variables to store the contents of each tag
private String m_sMsgFrom = "";
private String m_sMsgTo   = "";
private String m_sMsgCc   = "";
private String m_sMsgBCc  = "";
private String m_sMsgSub  = "";
private String m_sMsgBody = "";

/**
 * Constructor which calls the Baseclass Constructor
 */
public EMailParser()
{
    super();
}

/**
 * Print any text found inside an element.
 * @param chars  character array to be processed
 * @param iStart  the index from where the array has to be read
 * @param iLen  the length to which the array has to be read
 */

public void characters(char[] chars, int iStart, int iLen)
{
    String sValue = new String(chars, iStart, iLen);
    if(m_bFrom)
        m_sMsgFrom += sValue;
    else if(m_bTo)
        m_sMsgTo += sValue;
    else if(m_bCc)
        m_sMsgCc += sValue;
    else if(m_bBCc)
        m_sMsgBCc += sValue;
    else if(m_bSub)
        m_sMsgSub += sValue;
    else if(m_bBody)
        m_sMsgBody += sValue;
}

/**
 * function which returns the whole message
 * @return emessage the full message
 */

public EMessage getMessage()
{
    return new EMessage(m_sMsgFrom,m_sMsgTo,m_sMsgCc,m_sMsgBCc,m_sMsgSub,m_sMsgBody);
}
/**
 * Mark the element end and make the corresponding flag false.
 * @param element  the tag name
 */

public void endElement(String element)
{
    if (element.equals("xmlMessageFrom"))
    {
        m_bFrom = false;
    }
    else if(element.equals("xmlMessageTo"))
    {
        m_bTo = false;
    }
    else if(element.equals("xmlMessageCc"))
    {
        m_bCc = false;
    }
    else if(element.equals("xmlMessageBCc"))
    {
        m_bBCc = false;
    }
    else if(element.equals("xmlMessageBody"))
    {
        m_bBody = false;
    }
    else if(element.equals("xmlMessageSubject"))
    {
        m_bSub = false;
    }
}

/**
 * Mark the element start and make the corresponding flag true
 * @param element  the tag name
 * @param attrlist  attributes of the tag
 */

public void startElement(String element, org.xml.sax.AttributeList attrlist)
{
    if (element.equals("xmlMessageFrom"))
    {
        m_bFrom = true;
    }
    else if(element.equals("xmlMessageTo"))
    {
        m_bTo = true;
    }
    else if(element.equals("xmlMessageCc"))
    {
        m_bCc = true;
    }
    else if(element.equals("xmlMessageBCc"))
    {
        m_bBCc = true;
    }
    else if(element.equals("xmlMessageBody"))
    {
        m_bBody = true;
    }
    else if(element.equals("xmlMessageSubject"))
    {
        m_bSub = true;
    }
}

}

4

0 回答 0