Convert a string to XML input stream in java

后端 未结 3 1102
Happy的楠姐
Happy的楠姐 2021-02-02 12:14

I\'m trying to generate a PDF document using FOP and Java.

I receive the XML as a string and not as a file.

How can I convert this XML string to an XML input str

相关标签:
3条回答
  • 2021-02-02 12:46

    Use ByteArrayInputStream:

    String S = ...;
    InputStream source = new ByteArrayInputStream(S.getBytes(encoding))
    
    0 讨论(0)
  • 2021-02-02 12:48

    You probably want to convert it to a Reader, not an InputStream. Use StringReader to do this. StreamSource has a constructor that takes a Reader, and you can pass that StreamSource to Transformer.transform().

    I say you probably want a Reader rather than an InputStream because a String holds characters, not bytes, and an InputStream is a stream of bytes while a Reader is a stream of characters.

    0 讨论(0)
  • 2021-02-02 12:52
    new StreamSource(new StringReader(str))
    
    0 讨论(0)
提交回复
热议问题