2014-12-22 22 views
0
URL stringfile = getXsl("test.xml"); 
File originFile = new File(stringfile.getFile()); 

String xml = null; 
ByteArrayOutputStream pdfStream = null; 
try { 
FileInputStream fis = new FileInputStream(originFile); 
int length = fis.available(); 
byte[] readData = new byte[length]; 

fis.read(readData); 
xml = (new String(readData)).trim(); 
fis.close();    
xml = xml.substring(xml.lastIndexOf("<HttpCommandList>")+17, xml.lastIndexOf("</HttpCommandList>")); 
String[] splitxml = xml.split("</HttpCommand>"); 

for (int i = 0; i < splitxml.length; i++) { 
    tmpxml = splitxml[i].trim() + "</HttpCommand>"; 
    System.out.println("splitxml:" +tmpxml); 

    pdfStream = new ByteArrayOutputStream(); 
    pdf = new com.lowagie.text.Document(); 
    PdfWriter.getInstance(pdf, pdfStream); 
    pdf.open(); 

    URL xslToUse = getXsl("test.xsl"); 

    // Here am using some utility class to transform        
    // generate the XML needed by iText to generate the PDF using MessageBuffer contents 
    String iTextXml = XmlUtil.transformXml(tmpxml.toString(), xslToUse).trim(); 

    // generate the PDF document by parsing the specified XML file 
    XmlParser.parse(pdf, new ByteArrayInputStream(iTextXml.getBytes())); 

} 

對於上面的代碼中,我越來越java.net.malformedURL例外:無協議 我試圖通過解析指定的XML文件來生成PDF文檔。的XmlParser的過程中java.net.malformedURL例外

+0

你可以打印stringfile和xslToUse並讓我們知道輸出是什麼? – SMA

+2

您需要升級iText和XML Worker。如果您使用過時的'com.lowagie。*'包,這永遠無法正常工作。 (如果你懷疑我,看看我的名字。) –

回答

0

我們可能需要實際的xml文件來決定缺少的內容。我預計,沒有協議定義,就像這樣:

192.168.1.2/   (no protocol) 
file://192.168.1.2/ (there is one) 

而URL似乎需要一個。

相關搜索:

new File("somexsl.xlt").toURI().toURL(); 

herehere

它總是有助於搞砸整個堆棧跟蹤。沒有人知道,如果您不發佈行號,實際發生異常的地方。