简体   繁体   中英

Should I use DomDocument for parsing html code

I have tried my best to answer this question myself through research but I am still a little bit worried about whether I am using the right thing. Basically I am using the DomDocument Library to build a jQuery like theme parser for my framework. Now with the web as it is today HTML is coming in different shapes and sizes eg HTML 4, HTML 5, XHTML, XHTML 5 etc ... The issue that I am finding with DomDocument is that if you give it HTML code it will only work with it if it is standards compliant XHTML. I know it can convert it into XHTML and I can use the tidy library to make the code acceptable but my main worry is: what if a developer using my framework has a theme that uses all the cool(debatable) new HTML 5 features, as soon as he passes it to my framework it will either throw a tantrum or convert it down to XHTML which would suck.

So my question is: Is DomDocument the most convenient library for what I need?

or

Is there a way of getting it to work with all the different variants of HTML?

DOMDocument can parse non-XHTML files. Just set the proper switches:

    libxml_use_internal_errors ( true );

    $dom = new DOMDocument;
    $dom -> formatOutput = true;
    $dom -> substituteEntities = false;
    $dom -> recover = true;
    $dom -> strictErrorChecking = false;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM