2009-09-30 18 views

Odpowiedz

1

Znalazłem gdzieś (nie pamiętam gdzie):

public static DocumentFragment parseXml(Document doc, String fragment) 
{ 
    // Wrap the fragment in an arbitrary element. 
    fragment = "<fragment>"+fragment+"</fragment>"; 
    try 
    { 
     // Create a DOM builder and parse the fragment. 
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
     Document d = factory.newDocumentBuilder().parse(
       new InputSource(new StringReader(fragment))); 

     // Import the nodes of the new document into doc so that they 
     // will be compatible with doc. 
     Node node = doc.importNode(d.getDocumentElement(), true); 

     // Create the document fragment node to hold the new nodes. 
     DocumentFragment docfrag = doc.createDocumentFragment(); 

     // Move the nodes into the fragment. 
     while (node.hasChildNodes()) 
     { 
      docfrag.appendChild(node.removeChild(node.getFirstChild())); 
     } 
     // Return the fragment. 
     return docfrag; 
    } 
    catch (SAXException e) 
    { 
     // A parsing error occurred; the XML input is not valid. 
    } 
    catch (ParserConfigurationException e) 
    { 
    } 
    catch (IOException e) 
    { 
    } 
    return null; 
} 
3

You could use Swing:

jaki sposób skorzystać z możliwości HTML-przetwórczych, które są wbudowane w Java? Możesz nie wiedzieć, że Swing zawiera wszystkie klasy niezbędne do parsowania HTML. Jeff Heaton pokazuje, jak.

6

można użyć analizatora składni HTML, który biblioteka Java używana do parsowania HTML w sposób liniowy lub zagnieżdżony. Jest to narzędzie open source i można je znaleźć na SourceForge

9

oto sposób:

import java.io.*; 
import javax.swing.text.*; 
import javax.swing.text.html.*; 
import javax.swing.text.html.parser.*; 

public class HtmlParseDemo { 
    public static void main(String [] args) throws Exception { 
     Reader reader = new StringReader("<table><tr><td>Hello</td><td>World!</td></tr></table>"); 
     HTMLEditorKit.Parser parser = new ParserDelegator(); 
     parser.parse(reader, new HTMLTableParser(), true); 
     reader.close(); 
    } 
} 

class HTMLTableParser extends HTMLEditorKit.ParserCallback { 

    private boolean encounteredATableRow = false; 

    public void handleText(char[] data, int pos) { 
     if(encounteredATableRow) System.out.println(new String(data)); 
    } 

    public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) { 
     if(t == HTML.Tag.TR) encounteredATableRow = true; 
    } 

    public void handleEndTag(HTML.Tag t, int pos) { 
     if(t == HTML.Tag.TR) encounteredATableRow = false; 
    } 
} 
+0

Co zrobić, jeśli chcę umieścić wszystkie dane w tablicy w zewnętrznej klasie, zamiast je wydrukować? – CodyBugstein

+0

@Image, idź prosto, masz moje pozwolenie na umieszczenie ich w jakiejś kolekcji zamiast je drukować :) –

+0

Umieściłem je w kolekcji wewnątrz klasy 'HTMLTableParser', a następnie utworzyłem metodę getter, aby je uzyskać. Czy to najlepszy sposób na zrobienie tego? – CodyBugstein

5

Jeśli masz ciąg, który zawiera HTML można użyć Jsoup Taka biblioteka pozwala uzyskać elementy HTML:

String htmlTable= "<table><tr><td>Hello World!</td></tr></table>"; 
Document doc = Jsoup.parse(htmlTable); 

// then use something like this to get your element: 
Elements tds = doc.getElementsByTag("td"); 

// tds will contain this one element: <td>Hello World!</td> 

Powodzenia!

+0

Ta biblioteka po prostu wykonuje pracę, dzięki! – negstek

Powiązane problemy