我知道这个问题之前已被问过几次,但是这些问题似乎都不适合我。 我正在尝试从rss创建新闻源并将其写入pdf。 pdf已创建,但是空了,我得到了一个 (错误的位置未知)com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException:3字节UTF-8序列的无效字节2。 错误。 这些是我的课程:
获取Feed:
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
public class GetFeed {
private String adress;
private URL url;
public GetFeed(String adress) {
super();
this.adress = adress;
try {
setUrl();
} catch (MalformedURLException e) {
System.out.println("This isn't a correct url");
e.printStackTrace();
}
}
public void setUrl() throws MalformedURLException {
url = new URL(adress);
}
public SyndFeed getFeed() throws IOException, IllegalArgumentException,
FeedException {
HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(new XmlReader(httpcon));
return feed;
}
feedropation示例:
public void homeland() {
GetFeed homeland = new GetFeed("http://www.standaard.be/rss/section/1f2838d4-99ea-49f0-9102-138784c7ea7c");
try {
feed = homeland.getFeed();
WriteToXml xml = new WriteToXml(feed);
} catch (IllegalArgumentException | IOException |FeedException e) {
e.printStackTrace();
}
}
写下xmlfile:
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedOutput;
public class WriteToXml {
public WriteToXml(SyndFeed feed) throws IOException, FeedException {
Writer writer = new FileWriter("newsfeed.xml", true);
SyndFeedOutput output = new SyndFeedOutput();
output.output(feed, writer);
writer.close();
}
}
创建pdf
public class CreatePdf {
public void convertToPDF() throws IOException, FOPException, TransformerException {
File xsltFile = new File("template.xsl");
StreamSource xmlSource = new StreamSource(new File("newsfeed.xml"));
FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
OutputStream out;
out = new java.io.FileOutputStream("newsfeed.pdf");
try {
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(xsltFile));
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(xmlSource, res);
} finally {
out.close();
}
}
}
mainapp:
import java.io.IOException;
import javax.xml.transform.TransformerException;
import org.apache.fop.apps.FOPException;
public class NewsfeedApp {
public static void main(String[] args) {
CreateFeeds feeds = new CreateFeeds();
CreatePdf pdf = new CreatePdf();
try {
pdf.convertToPDF();
} catch (FOPException | IOException | TransformerException e) {
e.printStackTrace();
}
}
}
Anny帮助将不胜感激 抱歉蹩脚的英语,我不是本地人。