2014-07-17 5 views
5

मैं this function का उपयोग कर, कुछ स्क्रैप किए गए HTML को वैध xml में पार्स करने का प्रयास कर रहा हूं।मैं अपने स्क्रैप किए गए HTML को XML में क्यों नहीं पा सकता?

मेरे परीक्षण कोड (htmlParse समारोह की नकल की और बेन नाडेल के ब्लॉग से चिपकाया के साथ):

<cfscript> 
    // I take an HTML string and parse it into an XML(XHTML) 
    // document. This is returned as a standard ColdFusion XML 
    // document. 
    function htmlParse(htmlContent, disableNamespaces = true){ 

     // Create an instance of the Xalan SAX2DOM class as the 
     // recipient of the TagSoup SAX (Simple API for XML) compliant 
     // events. TagSoup will parse the HTML and announce events as 
     // it encounters various HTML nodes. The SAX2DOM instance will 
     // listen for such events and construct a DOM tree in response. 
     var saxDomBuilder = createObject("java", "com.sun.org.apache.xalan.internal.xsltc.trax.SAX2DOM").init(); 

     // Create our TagSoup parser. 
     var tagSoupParser = createObject("java", "org.ccil.cowan.tagsoup.Parser").init(); 

     // Check to see if namespaces are going to be disabled in the 
     // parser. If so, then they will not be added to elements. 
     if (disableNamespaces){ 

     // Turn off namespaces - they are lame an nobody likes 
     // to perform xmlSearch() methods with them in place. 
     tagSoupParser.setFeature(
     tagSoupParser.namespacesFeature, 
     javaCast("boolean", false) 
     ); 

     } 

     // Set our DOM builder to be the listener for SAX-based 
     // parsing events on our HTML. 
     tagSoupParser.setContentHandler(saxDomBuilder); 

     // Create our content input. The InputSource encapsulates the 
     // means by which the content is read. 
     var inputSource = createObject("java", "org.xml.sax.InputSource").init(
     createObject("java", "java.io.StringReader").init(htmlContent) 
     ); 

     // Parse the HTML. This will trigger events which the SAX2DOM 
     // builder will translate into a DOM tree. 
     tagSoupParser.parse(inputSource); 

     // Now that the HTML has been parsed, we have to get a 
     // representation that is similar to the XML document that 
     // ColdFusion users are used to having. Let's search for the 
     // ROOT document and return is. 
     return(
     xmlSearch(saxDomBuilder.getDom(), "/node()")[ 1 ] 
     ); 

    } 
</cfscript> 
<cfset html='<tr > <td align="center"> <span id="id1" >Compliance Review</span> </td><td class="center"> <span id="id2" >395.8(i)</span> </td><td align="left"> <span id="id3" >Failing to submit a record of duty status within 13 days </span> </td><td class="center" > <span id="id4">4/17/2014</span> </td> </tr>' /> 
<cfset parsedData = htmlParse(html) /> 

(एचटीएमएल एक अलग समारोह से इस प्रारूप में प्राप्त होता है, लेकिन मैं करने के लिए अब के लिए स्ट्रिंग हार्डकोड करने की कोशिश की समस्या का पता लगाने)

मैं मिल निम्न त्रुटि:।

NOT_FOUND_ERR: An attempt is made to reference a node in a context where it does not exist. 
The error occurred in myfilePath/myfileName.cfm: line 42 

40 :  // Parse the HTML. This will trigger events which the SAX2DOM 
41 :  // builder will translate into a DOM tree. 
42 :  tagSoupParser.parse(inputSource); 

क्या गलत हो रहा है? मैं इसे कैसे ठीक कर सकता हूं?

+0

आप ColdFusion का उपयोग कर रहे हैं तरीकों कॉल करने के लिए, आप कर रहे हैं सीधे जावा का उपयोग कर। यह सिर्फ मुझे लगता है जैसे इनपुट अच्छी तरह से स्वरूपित नहीं है या पार्सर में एक बग है। –

+0

@ जेटी। - यह मुद्दा नहीं है कि इसे "गंदे" एचटीएमएल के लिए भी काम करना चाहिए और इनपुट को अच्छी तरह से स्वरूपित नहीं किया जाना चाहिए? – froadie

+1

@froadie टैग्स के मुद्दों के बारे में निश्चित नहीं है, लेकिन मैंने Jsoup का उपयोग किया है और यह अच्छी तरह से काम करता है। ये दो लिंक आसान हो सकते हैं। http://www.raymondcamden.com/2012/4/6/jsoup-adds-jQuerylike-parsing-in- जावा और http://www.bennadel.com/blog/2358-parsing-traversing-and-mutating- html-with-coldfusion-and-jsoup.htm –

उत्तर

2

मैं TagSoup उपयोग नहीं किया है, लेकिन मैं महान परिणामों के साथ साल के लिए jTidy उपयोग कर रहा है (एमएस वर्ड सहित) के सूत्रों के सभी प्रकार से उपयोगकर्ता द्वारा प्रदान की एचटीएमएल लेते हैं और इसे साफ ऐसी है कि वह एक्सएचटीएमएल देता है।

आप जेटीडी जार को अपने क्लासपाथ पर छोड़कर या लोड करने के लिए जावालोडर का उपयोग करके उसी दस्तावेज़ पर jTidy को आजमा सकते हैं। चूंकि आप CF10 पर हैं, आप this method to include the JAR का उपयोग कर सकते हैं।

फिर, यहाँ कैसे cfscript में jTidy कॉल करने के लिए है:

jTidy = createObject("java", "org.w3c.tidy.Tidy"); 

jTidy.setQuiet(false); 
jTidy.setIndentContent(true); 
jTidy.setSmartIndent(true); 
jTidy.setIndentAttributes(true); 
jTidy.setWraplen(1024); 
jTidy.setXHTML(true); 
jTidy.setNumEntities(true); 
jTidy.setConvertWindowsChars(true);    
jTidy.setFixBackslash(true);  // changes \ in urls to/
jTidy.setLogicalEmphasis(true);  // uses strong/em instead of b/i 
jTidy.setDropEmptyParas(true); 

// create the in and out streams for jTidy 
readBuffer = CreateObject("java","java.lang.String").init(parseData).getBytes(); 
inP = createobject("java","java.io.ByteArrayInputStream").init(readBuffer); 
outx = createObject("java", "java.io.ByteArrayOutputStream").init(); 

// do the parsing 
jTidy.parse(inP,outx); 
outstr = outx.toString(); 

यह मान्य एक्सएचटीएमएल जो आप XPath के साथ के खिलाफ क्वेरी कर सकता है वापस आ जाएगी। मैं एक makeValid() फ़ंक्शन में ऊपर लिपटे और फिर इसे अपने HTML के खिलाफ भाग गया:

<cfset html='<tr > <td align="center"> <span id="id1" >Compliance Review</span> </td><td class="center"> <span id="id2" >395.8(i)</span> </td><td align="left"> <span id="id3" >Failing to submit a record of duty status within 13 days </span> </td><td class="center" > <span id="id4">4/17/2014</span> </td> </tr>' /> 
<cfset out = makeValid(html) /> 
<cfdump var="#xmlParse(out)#" /> 

और यहाँ था उत्पादन:

Picture of the cfdump output from xmlParse()

+0

मैं वास्तव में @ गौरव्स की टिप्पणी के अनुसार जेएसओपी का उपयोग कर समाप्त हुआ ... लेकिन इसके लिए धन्यवाद, और जार को शामिल करने के बारे में लिंक भी उपयोगी था। – froadie

संबंधित मुद्दे