0

I used the transformer code mentioned in below link to convert xml element and its child to xml string

How to convert xml Element and its child nodes into String in Java?

The problem is i am seeing some weird characters , only first element text content is in plain text rest is some weird encoding

For quick reference here is the code

Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes");  
StreamResult result = new StreamResult(new StringWriter()); 
DOMSource source = new DOMSource(node);
transformer.transform(source, result);
String xmlString = result.getWriter().toString(); System.out.println(xmlString); 

Input looks like below

<Info ID="_65ae9406-63c9-4fd5-93a0-5ab1b5a3f3c7"
            IssueInstant="2012-02-11T09:53:52.722Z" Version="2.0"
            xmlns="urn:oasis:names:tc:SAML:2.0:assertion">
            <Issuer>https://localhost:8080/</Issuer>
            <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
                <ds:SignedInfo>
                    <ds:CanonicalizationMethod
                        Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
                    <ds:SignatureMethod
                        Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
                    <ds:Reference URI="#_65ae9406-63c9-4fd5-93a0-5ab1b5a3f3c7">
                        <ds:Transforms>
                            <ds:Transform
                                Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
                            <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
                        </ds:Transforms>
                        <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
                        <ds:DigestValue>IDHt1dgdR9k/toaocOxCVMiJhPjqRPyNSou4ywBA2YM=
                        </ds:DigestValue>
                    </ds:Reference>
                </ds:SignedInfo>
                <ds:SignatureValue>o5lTezATnGX+sllItfElMuabaFlpBed8wc8lAieigQ4JiIsgjIdHbg7gyyCpgk</ds:SignatureValue>

            <ds:Signature></Info>
1
  • What does the input look like? Commented May 3, 2012 at 23:49

2 Answers 2

0

Try adding an XML declaration which specifies the encoding of your XML file (UTF-8 in the example). Maybe it helps :)

<?xml encoding="UTF-8"?>
Sign up to request clarification or add additional context in comments.

1 Comment

The XML i posted is actually the content of org.w3c.dom.Element , so there is no way i can add declaration
0

Look at: DOMImplementationLS serialize to String in UTF-8 in Java

Notice this piece:

StringWriter output = new StringWriter();

and:

String xml = output.toString();

The example above is calling toString on the string writer, you're calling it on the streamResult.

Trying breaking it out like that example does and see if that works for you.

1 Comment

No same issue , doesn't help. Thanks for your time

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.