无法分析形成不良的XML(Cannot parse poorly formed XML)
我一直试图解析这个提要 。 如果你点击该链接,你会发现它甚至无法在浏览器中正确解析它。
无论如何,我的托管服务不会让我使用simplexml_load_file,所以我一直使用cURL来获取它,然后将字符串加载到DOM中,如下所示:
$dom = new DOMDocument; $dom->loadXML($rawXML); if (!$dom) { echo 'Error while parsing the document'; exit; } $xml = simplexml_import_dom($dom);
但我得到错误(“DOMDocument :: loadXML()[domdocument.loadxml]:实体'nbsp'没有在实体中定义”),然后我尝试使用SimpleXMLElement没有运气(它显示相同的错误“解析器错误:实体'nbsp'未定义“等等,因为那个元素中的HTML)。
$xml = new SimpleXMLElement($rawXML);
所以我的问题是,如何跳过/忽略/移除该元素,以便我可以解析其余的数据?
编辑:感谢mjv的解决方案!...我只是这样做(对于其他人有同样的麻烦)
$rawXML = str_replace('<description>','<description><![CDATA[',$rawXML); $rawXML = str_replace('</description>',']]></description>',$rawXML);
I've been trying to parse this feed. If you click on that link, you'll notice that it can't even parse it correctly in the browser.
Anyway, my hosting service won't let me use simplexml_load_file, so I've been using cURL to get it then loading the string into the DOM, like this:
$dom = new DOMDocument; $dom->loadXML($rawXML); if (!$dom) { echo 'Error while parsing the document'; exit; } $xml = simplexml_import_dom($dom);
But I get errors ("DOMDocument::loadXML() [domdocument.loadxml]: Entity 'nbsp' not defined in Entity"), then I tried using SimpleXMLElement without luck (it shows the same error "parser error : Entity 'nbsp' not defined", etc... because of the HTML in that one element).
$xml = new SimpleXMLElement($rawXML);
So my question is, how do I skip/ignore/remove that element so I can parse the rest of the data?
Edit: Thanks to mjv for the solution!... I just did this (for others that have the same trouble)
$rawXML = str_replace('<description>','<description><![CDATA[',$rawXML); $rawXML = str_replace('</description>',']]></description>',$rawXML);
原文:https://stackoverflow.com/questions/1518142
最满意答案
是的,你认为POI会这样做是错误的。 Apache POI适用于Microsoft Office文件格式,而PDF则不适用。
您要么直接使用Apache PDFBox ,要么使用Apache Tika ,它将同时使用Microsoft Office和PDF文件格式(以及许多其他格式)。
Yes, you are wrong in believing that POI will do that. Apache POI works with Microsoft Office file formats, which PDF isn't.
You'll either want to use Apache PDFBox directly, or us Apache Tika which will do both Microsoft Office and PDF file formats (amongst many others).
相关问答
更多-
Apache POI性能(Apache POI Performance)[2021-11-08]
测试结果在我的机器上: HSSF:2秒 SXSSF:5秒 XSSF:27秒 机器规格: CPU: Intel i3-2100, 3.10 GHz, 4-cores RAM: 16GB OS: Windows 7 64bit JDK: 1.7.0_76 我进行了分析,发现由于xmlbeans和poi-ooxml-schemas库中的同步方法, xmlbeans速度很慢。 您可以通知poi开发人员并要求检查此案例。 Test results on my machine: HSSF: 2 sec SXSSF: ... -
apache poi从右到左(right to left with apache poi)[2022-04-05]
我重新解决了问题,这很简单,问题是由于我用阿拉伯语替换法语单词,因此他们只是用来定位我必须修改文本的地方,他们可以是任何一个charchter,更合适的是阿拉伯语! i resloved the problem it's so simple , the problem is due that i replace french word with arab ones and since this they are just a words used to locate where i have to modi ... -
使用Apache poi设置边距(Set margins with Apache poi)[2023-01-30]
感谢Gagravarr指出我正确的方向。 似乎我没有完整的ooxml-schemas-1.1.jar。 此代码现在可以完美地设置边距 CTSectPr sectPr = document.getDocument().getBody().addNewSectPr(); CTPageMar pageMar = sectPr.addNewPgMar(); pageMar.setLeft(BigInteger.valueOf(720L)); pageMar.setTop(BigInteger.valu ... -
如果您的组织(或客户)处于以下情况,Jasper可以带来好处: 无法承受更强大的解决方案,如Crystal Reports(或Cognos等) 需要高度定制的/用户可配置的临时报告解决方案,这些解决方案非常易于集成到现有应用程序中(基于Web或桌面):思考“嵌入式”解决方案。 我使用Jasper Reports作为后端报告生成器(内部Jasper使用Apache POI生成XLS,使用iText生成PDF)构建了一个完全动态的,基于Web的临时报告解决方案。 其他报告包是否可以使用? 我相信它会有,但整合本 ...
-
是的,你认为POI会这样做是错误的。 Apache POI适用于Microsoft Office文件格式,而PDF则不适用。 您要么直接使用Apache PDFBox ,要么使用Apache Tika ,它将同时使用Microsoft Office和PDF文件格式(以及许多其他格式)。 Yes, you are wrong in believing that POI will do that. Apache POI works with Microsoft Office file formats, whic ...
-
如何使用Apache POI从MS word文档的文本框中获取文本?(How to get text from textbox of MS word document using Apache POI?)[2022-01-19]
这对我有用, private void printContentsOfTextBox(XWPFParagraph paragraph) { XmlObject[] textBoxObjects = paragraph.getCTP().selectPath(" declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' declare namespace w ... -
Apache POI和Aspose?(Apache POI and Aspose?)[2022-02-28]
任何人都可以向我建议我应该去哪一个? Apache POI和Aspose有哪些限制? 这是非常困难和普遍的问题,只能有非常普遍的答案。 每个软件项目都有不同的要求和功能。 对于每个项目来说,使用第三方组件的可行性很可能也不同。 要选择不同的第三方很困难,因为您需要做更多或更少的事情 需求评估(哪个产品符合您的要求或者紧密满足) 在购买产品之前和之后看看客户支持有多好 第三方产品的功能比较 找到多少稳定的产品。 检查他们发布了多少个版本。 新版本有错误修复,新功能? 独立来源的任何奖项 API和文档的总体可用 ... -
如果你获取一行,并返回null ,那么这意味着该行的文件中没有存储数据 - 它完全是空白的。 POI默认为您提供文件中的内容。 使用Cells,您可以设置MissingCellPolicy来控制丢失和空白单元格的处理方式。 在Apache POI文档中有一些使用它的例子。 对于行,它们是否存在,因此在获取行时需要检查空值。 If you fetch a row, and get back null, then that means there is no data stored in the file fo ...
-
Java Apache POI(Java Apache POI)[2022-11-01]
1.在java中解决OutOfMemoryError的简单方法是使用JVM选项"-Xmx512M"来增加最大堆大小,这将立即解决你的OutOfMemoryError。 Java虚拟机(JVM)以固定的内存上限运行,您可以对其进行修改: -Xms- Set initial Java heap size -Xmx - Set maximum Java heap size $ java -Xms512m -Xmx1024m JavaApp 2.解决Java中的OutOfMemoryE ... -
您在想, RichTextString应该能够解释HTML吗? 不是这样。 它只能将 startIndex Font应用于endIndex 。 可以使用TypeOffset SS_SUPER标记字体 。 例: import org.apache.poi.xssf.usermodel.*; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.*; import java.io.FileOutputStream; clas ...