dongmacuo1193 2012-06-05 22:44 采纳率: 0%
浏览 26

验证并可能更正我拥有的XML文件

I have an XML file (random resource ) that I need to use. I am using PHP DOM Document to process this document and DOMXPath to query the object. However, the XPath queries are not returning any kind of data (resultset->length = 0). The query is correct, and I am not worried about the syntax to it. I also experimented with DOMDOcument->getElementsByTagName("tag") and this returns a null result as well.

In the context above, how can I check if my DOMDocument was able to load when I did call the

DOMDocument->Load('file.xml')


[also tried 

$xmlFile = file_get_contents('file.xml')
DOMDocument->loadXML($xmlFile)]

I am assuming if the XML file is fault then in all likelihood I cannot query the DOMDocument at all ? Is there anyway for me to check if the a) the document was loaded at all when I called DOMDocument->Load/loadXML methods b) validate the XML file using external resources (w3 schools validator is the only one I know) c) if the XML file is incorrect, anyway that I can rectify it ?

Thanks to all those who take the time out and read this.

Best, Parijat Kalia

  • 写回答

1条回答 默认 最新

  • dongping8572 2012-06-08 13:41
    关注

    Try flipping this on before doing anything:

    libxml_use_internal_errors(true);

    Then after loading your XML, look for errors using:

    $errors = libxml_get_errors();

    You can iterate over $errors (it's an array of libXMLError objects).

    For example:

    libxml_use_internal_errors(true);
    
    try
    {
        $doc = new DOMDocument();
        $doc->load("somebadxmlfile.xml");
    }
    catch (Exception $e)
    {
        $errors = libxml_get_errors();
    
        foreach ($errors as $error)
        {
            echo $error->message . " [ Line: " . $error->line . " | Column: " . $error->column . " | Level: " . $error->level . " | File: " . $error->file . " ]";
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图