首页 \ 问答 \ 无法分析形成不良的XML(Cannot parse poorly formed XML)

无法分析形成不良的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
更新时间:2024-03-26 18:03

最满意答案

是的,你认为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).

相关问答

更多
  • 测试结果在我的机器上: 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: ...
  • 我重新解决了问题,这很简单,问题是由于我用阿拉伯语替换法语单词,因此他们只是用来定位我必须修改文本的地方,他们可以是任何一个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 ...
  • 感谢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 ...
  • 这对我有用, 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有哪些限制? 这是非常困难和普遍的问题,只能有非常普遍的答案。 每个软件项目都有不同的要求和功能。 对于每个项目来说,使用第三方组件的可行性很可能也不同。 要选择不同的第三方很困难,因为您需要做更多或更少的事情 需求评估(哪个产品符合您的要求或者紧密满足) 在购买产品之前和之后看看客户支持有多好 第三方产品的功能比较 找到多少稳定的产品。 检查他们发布了多少个版本。 新版本有错误修复,新功能? 独立来源的任何奖项 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 ...
  • 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 ...

相关文章

更多

最新问答

更多
  • Runnable上的NetworkOnMainThreadException(NetworkOnMainThreadException on Runnable)
  • C ++ 11 + SDL2 + Windows:多线程程序在任何输入事件后挂起(C++11 + SDL2 + Windows: Multithreaded program hangs after any input event)
  • AccessViolationException未处理[VB.Net] [Emgucv](AccessViolationException was unhandled [VB.Net] [Emgucv])
  • 计算时间和日期差异(Calculating Time and Date difference)
  • 以编程方式标签NSMutableAttributedString swift 4(Label NSMutableAttributedString programmatically swift 4)
  • C#对象和代码示例(C# objects and code examples)
  • 在python中是否有数学nCr函数?(Is there a math nCr function in python? [duplicate])
  • 检索R中列的最大值和第二个最大值的行名(Retrieve row names of maximum and second maximum values of a column in R)
  • 给定md5哈希时如何查找特定文件(How to find specific file when given md5 Hash)
  • Python字典因某些原因引发KeyError(Python Dictionary Throwing KeyError for Some Reason)
  • 如何让Joomla停止打开新标签中的每个链接?(How do I get Joomla to stop opening every link in a new tab?)
  • DNS服务器上的NS记录不匹配(Mismatched NS records at DNS server)
  • Python屏幕捕获错误(Python screen capture error)
  • 如何在帧集上放置div叠加?(How to put a div overlay over framesets?)
  • 页面刷新后是否可以保留表单(html)内容数据?(Is it possible to retain the form(html) content data after page refreshed?)
  • 使用iTeardownMyAppFrame和iStartMyAppInAFrame在OPA5测试中重新启动应用程序超时(Restart app within OPA5 test using iTeardownMyAppFrame and iStartMyAppInAFrame timed out)
  • 自动拆分文本内容到列(Automatically splitting text content into even columns)
  • 在r中的循环中将模型名称分配给gbm.step(assigning model names to gbm.step in loop in r)
  • 昆明哪里有电脑等级考试二级C培训?
  • C ++模板实例化,究竟是什么意思?(C++ template instantiation, what exactly does it mean?)
  • 帮助渲染来自fields_for的部分内容(Help to render a partial from fields_for)
  • 将url.action作为json对象返回mvc(return url.action as json object mvc)
  • 使用.BAT中的.application文件类型运行ac#Console App(Run a c# Console App with .application file type from a .BAT)
  • 将bindingRedirect添加到.Net标准库(Adding a bindingRedirect to a .Net Standard library)
  • Laravel版本升级会影响您的控制器吗?(Laravel version upgrade affects your controller?)
  • imaplib.error:命令SEARCH在状态AUTH中非法,只允许在SELECTED状态(imaplib.error: command SEARCH illegal in state AUTH, only allowed in states SELECTED)
  • 如何在eclipse debug impala前端
  • 如何通过Ajax API处理多个请求?(How to handle multiple requests through an Ajax API? [closed])
  • 使用Datetime索引来分析数据框数据(Using Datetime indexing to analyse dataframe data)
  • JS 实现一个菜单效果