如何将javax.xml.transform.dom.DOMResult()转换为String

时间:2017-01-25 06:44:49

标签: java xml unicode javax

我遇到了问题,其中XML格式包含名字中的日文字符,然后将其转换为ByteArrayInputStream,然后将该Array传递给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>ガネーシュ</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;
    }
}

}

0 个答案:

没有答案