2016-07-22 52 views
0

我使用此代碼來下載Xml文件。

String url="https://www.sec.gov/Archives/edgar/data/16160/000001616016000061/calm-20160528.xml"; 

      String fileName = url.substring(url.lastIndexOf("/") + 1, 
        url.length()); 

      String completeFileLocationWithName="/home/user/Downloads/XBRLCODE/"+fileName; 

      URL surl = new URL(url); 
      con = surl.openConnection(); 
      con.setConnectTimeout(0); 
      con.setReadTimeout(0); 
      InputStream in = con.getInputStream(); 
      Files.copy(in, Paths.get(completeFileLocationWithName));*/ 

,並試圖與String escapedInput = StringEscapeUtils.escapeXml(appNameInput);

輸入:URL

輸出是在下載XML,它不應該有像上面這樣&lt;&gt;&amp;等人物 - 而不是<,>,&對我來說會很好..

請任何人分享這個知識..

+0

你是否還要檢查unescapeHtml方法StringEscapeUtils – HRgiger

+0

字符串escapedInput = StringEscapeUtils.unescapeHtml4(格); –

+0

@HRgiger試過..plz分享代碼 –

回答

0

使用StringEscapeUtils from commons-lang.jar庫。

這裏是工作代碼:

import java.io.IOException; 
import java.io.InputStream; 
import java.io.StringWriter; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLConnection; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import org.apache.commons.io.IOUtils; 
import org.apache.commons.lang.StringEscapeUtils; 

public class Test { 

    public static void main(String[] args) { 
     String url = "https://www.sec.gov/Archives/edgar/data/16160/000001616016000061/calm-20160528.xml"; 

     URL surl; 
     try { 
      surl = new URL(url); 
      URLConnection con = surl.openConnection(); 
      con.setConnectTimeout(0); 
      con.setReadTimeout(0); 
      InputStream in = con.getInputStream(); 
      StringWriter writer = new StringWriter(); 
      IOUtils.copy(in, writer, "UTF-8"); 
      System.out.println(StringEscapeUtils.unescapeHtml(writer.toString())); 
     } catch (MalformedURLException ex) { 
      Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); 
     } catch (IOException ex) { 
      Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); 
     } 

    } 
} 

輸出是沒有轉義字符,這裏是從控制檯樣本:

<td valign="bottom" style="width:02.96%;border-top:1pt none #D9D9D9 ;border-left:1pt none #D9D9D9 ;border-bottom:1pt none #D9D9D9 ;border-right:1pt none #D9D9D9 ;background-color: #auto;height:1.00pt;padding:0pt;"> 
        <p style="margin:0pt;font-family:Times New Roman;height:1.00pt;overflow:hidden;font-size:0pt;"> 
         &nbsp;</p> 
       </td> 
       <td valign="bottom" style="width:02.40%;border-top:1pt none #D9D9D9 ;border-left:1pt none #D9D9D9 ;border-bottom:1pt none #D9D9D9 ;border-right:1pt none #D9D9D9 ;background-color: #auto;height:1.00pt;padding:0pt;"> 
        <p style="margin:0pt;font-family:Times New Roman;height:1.00pt;overflow:hidden;font-size:0pt;"> 
         &nbsp;</p> 
       </td> 
       <td valign="bottom" style="width:11.82%;border-top:1pt none #D9D9D9 ;border-left:1pt none #D9D9D9 ;border-bottom:1pt none #D9D9D9 ;border-right:1pt none #D9D9D9 ;background-color: #auto;height:1.00pt;padding:0pt;"> 
        <p style="margin:0pt;font-family:Times New Roman;height:1.00pt;overflow:hidden;font-size:0pt;"> 
         &nbsp;</p> 
       </td> 

繼續記住,你需要:

import org.apache.commons.io.IOUtils; 
import org.apache.commons.lang.StringEscapeUtils; 
+0

試過@Hrbosch ...同樣的問題.share代碼 –

+0

@JohnAdam檢查更新,我認爲這是你需要的 – Hrabosch

0

我認爲你稍微誤解了這個問題。這裏的XML包含嵌入式 HTML(本身帶有嵌入式CSS,就像它發生的那樣)。

被列入該節點,這些轉義字符,否則整個XML就會無效(<>&等都是reserved entities in XML)。

如果你的意思是你想要的那XML節點us-gaap:FiscalPeriod聯合國逃脫的結果,那麼你應該提取它的字符串值,然後使用類似StringEscapeUtils.unescapeHtml作爲已經建議。

取決於你想要做什麼,你可能想要從輸出中繼續前進,並且strip all HTML tags

+0

我的問題是如何下載沒有這些文件的XML文件特殊字符。 –

+0

選擇其他XML文件?他們已經在那裏 - 你必須通過處理你的輸入來獲得你想要的輸出來處理它們。 – declension

+0

請我編輯代碼,如果任何人都可以共享代碼,那麼它非常有用....感謝 –

0

以下似乎工作。

InputStream iStream = new FileInputStream(new File("xxxxx")); 
    StringWriter writer = new StringWriter(); 
    IOUtils.copy(iStream, writer, "UTF-8"); 
    String theString = writer.toString(); 
    IOUtils.write(StringEscapeUtils.unescapeXml(theString), 
      new FileOutputStream("yyyy")); 
+0

其工作正常,但 是「yyyy」文件中的問題...你有任何其他解決方案來克服。 –

+0

我這樣做的特殊字符在HTML –

+0

中得到解決這個字符在HTML ....

相關問題