Parsing HTML characters (e.g. &nbsp)

Hi
Apologies if I'm missing something obvious, I haven't been able to find an answer searching the API or Forums...
I'm parsing HTML documents (currently as Strings) to extract certain information. Is there an easy way to replace all special HTML characters such as   < etc. to a space or < respectively without having to do a string replace on every possible HTML character?
I know there's an HTML parser in swing but that seems to be geared towards creating an HTML editor.
Any help would be appreciated!

There are also a number of open source or shareware programs, such as TidyHTML, that clean-up and parse existing HTML. Check out Sourceforge or www.downloads.com.
- Saish

Similar Messages

  • Special HTML Characters

    Hi,
    I encountered a problem with regards to the display of special HTML characters(chr 155). Crystal was not able to correctly display the cahracters. Instead a blank space was displayed. In addition to, when the report is exported to PDF, it is displayed as boxes.
    Is there a way to handle display of special HTML chars in crystal?
    Thanks

    Crystal HTML interpreter is very limited and has been same for years, so it seems unlikley it will chnage any time soon.
    As its a specific character that is failing use a replace formul to remove the long dash html and replace with a short dash html which I guess Crystal will recognise.
    Replace(yourfield, 'longdashhtml', 'shortdashhtml')
    Ian

  • JEditorPane parsing HTML

    Hi all,
    I am using JEditorPane and it's ability to parse HTML, which although is relatively old and crusty is certainly all I need for the job.
    Now, I understand there is a chain of classes involved in taking my .html file and turning popping into a something we can see in a JEditorPane. For example, an img tag, is picked up by HTMLEditorKit and turned into an ImageView for display purposes.
    I want to do the following: I have subclassed HTMLEditorKit, and have overridden the HTMLFactory (although at the moment it just defers everything to super). I want to be able to pick out all of the html comment tags as they go through the HTMLEditorKit :
    <!-- hey hey this is a comment -->... and get to the comment text, "hey hey this is a comment", as a Java string. However I've been digging around with Element for hours now and although my HTMLFactory correctly digs out the comments from the rest of the elements:
    else if (kind == HTML.Tag.COMMENT)
                        {System.out.println("I found a comment but don't know what it said!!");... as you can see, I don't know how to get to the comment text itself.
    The reason why I want access to the comment text is that I want to supplement the HTML code a little bit and add something in the comment that will affect the way it is rendered when I read it depending on the comment - so there's the reason if curious.
    Any help, and I do mean anything at all, would be much appreciated, as this is the last obstacle in my path to getting this thing working :)
    Thanks for your time!
    - Peter

    Here is some old code I have lying around that attempts to iterate through all the elements. If I remember correctly the comment text is found in the AttributeSet of the element:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.text.*;
    import javax.swing.text.html.*;
    class GetHTML
        public static void main(String[] args)
            EditorKit kit = new HTMLEditorKit();
            Document doc = kit.createDefaultDocument();
            // The Document class does not yet handle charset's properly.
            doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
            try
                // Create a reader on the HTML content.
                Reader rd = getReader(args[0]);
                // Parse the HTML.
                kit.read(rd, doc, 0);
                System.out.println( doc.getText(0, doc.getLength()) );
                System.out.println("----");
                // Iterate through the elements of the HTML document.
                ElementIterator it = new ElementIterator(doc);
                Element elem = null;
                while ( (elem = it.next()) != null )
                    AttributeSet as = elem.getAttributes();
                    System.out.println( "\n" + elem.getName() + " : " + as.getAttributeCount() );
                    if ( elem.getName().equals( HTML.Tag.IMG.toString() ) )
                        Object o = elem.getAttributes().getAttribute( HTML.Attribute.SRC );
                        System.out.println( o );
                    Enumeration enum = as.getAttributeNames();
                    while( enum.hasMoreElements() )
                        Object name = enum.nextElement();
                        Object value = as.getAttribute( name );
                        System.out.println( "\t" + name + " : " + value );
                        if (value instanceof DefaultComboBoxModel)
                            DefaultComboBoxModel model = (DefaultComboBoxModel)value;
                            for (int j = 0; j < model.getSize(); j++)
                                Object o = model.getElementAt(j);
                                Object selected = model.getSelectedItem();
                                if ( o.equals( selected ) )
                                    System.out.println( o + " : selected" );
                                else
                                    System.out.println( o );
                    if ( elem.getName().equals( HTML.Tag.SELECT.toString() ) )
                        Object o = as.getAttribute( HTML.Attribute.ID );
                        System.out.println( o );
                    //  Wierd, the text for each tag is stored in a 'content' element
                    if (elem.getElementCount() == 0)
                        int start = elem.getStartOffset();
                        int end = elem.getEndOffset();
                        System.out.println( "\t" + doc.getText(start, end - start) );
            catch (Exception e)
                e.printStackTrace();
            System.exit(1);
        // Returns a reader on the HTML data. If 'uri' begins
        // with "http:", it's treated as a URL; otherwise,
        // it's assumed to be a local filename.
        static Reader getReader(String uri)
            throws IOException
            // Retrieve from Internet.
            if (uri.startsWith("http:"))
                URLConnection conn = new URL(uri).openConnection();
                return new InputStreamReader(conn.getInputStream());
            // Retrieve from file.
            else
                return new FileReader(uri);
    }To test it just use:
    java GetHTML somefile.html

  • Parsing escape characters

    I have a query:
    //query
    String query_one = "UPDATE homepage SET section1='" + section1 + "' ";
    //execute query
    section1SQL.executeUpdate(query_one);
    Here's the problem: if the var + section1 + contains a single quote or an apostrophe - the SQL query trips up.
    Is there a function or way to parse the characters so that whatever is contained in the var can be submitted into the DB without tripping the SQL query?
    Any help is appreciated.
    Thanks
    Mark

    Hi Mark,
    1.
    Just take you sql and test it interactive against your database (sqlserver, oracle, db2, access, ect...)
    and see how it interprete it. Then based upon that, you can write a function:
    replace single ' by double ''
    replace something by something
    UPDATE homepage SET section1 = VALUE
    where VALUE = it's my day
    where VALUE = "you're right!"
    where VALUE = it's correctly said "Great!"
    2. In sqlserver, you need to escape single ' by double ''
    --Paul.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Parsing HTML documents

    I am trying to write an application that uses a parsed html document to perform some data retrieval. The problem that I am having is that the parser in JDK1.4.1 is unable to completely parse the document correctly. Some fields are skipped as well as other problems. I believe it has to do with the html32.bdtd. Is there a later version?

    Parsing a HTML document is a huge task, you shouldn't do it yourself but instead javax.text.html and javax.text.html.parser already provide almost everything you ever need

  • Parsing HTML files

    Hello,
    I have a question about parsing HTML files. Usually when I get an HTML file and I need to find all the text in it I do this. This stuff just collects all of the hyperlinks and ignores all the html tags just keeping the actual text. It's fine for smaller files but occasionally I'll hit a large online text file and it will work but its way to slow for large files. I don't need to do all of this HTML tag stripping however for text files. Is there a way to still grab all the text without doing any tag searching to make it faster?
    thanks,
    private void find() throws IOException
            //Really slow for large text files.  Need a way to just use a regular scanner on an internet text file
            new ParserDelegator().parse(new InputStreamReader(myBase.openStream()),
                    new ParserListener(),
                    true); 
         * Inner class for processing all "<a href.."> tags when reading a base URL.
        private class ParserListener extends HTMLEditorKit.ParserCallback
            final String IGNORED_LINKS = "^(http|mailto|\\W).*";
            public void handleStartTag (HTML.Tag t, MutableAttributeSet a, int pos)
                if (t == HTML.Tag.A)
                    String href = (String)(a.getAttribute(HTML.Attribute.HREF));
                    //System.out.println(href);
                    //System.out.println(href.matches(IGNORED_LINKS) + "\t" + href);
                    if (! (href == null || href.matches(IGNORED_LINKS)) && !myURLs.contains(href))
                        myURLs.add(href);
                //TODO fix
                if (t == HTML.Tag.TITLE)
                    String title = (String) (a.getAttribute(HTML.Attribute.TITLE));
                    if (!(title == null))
                        myTitle = title;
                    else myTitle = "No title was found";
            public void handleText (char[] data, int pos)
                myText.append(" ");
                myText.append(data);
        }

    JFactor2004 wrote:
    My question is. If I know an html file is actually just a txt fileThis isn't a question. HTML files are text by definition.
    is it possible to look through it (maybe use something similar to a regular scanner) without doing anything with html.That depends on what you mean by "doing something with HTML". You can certainly read it one line at a time.

  • Parsing HTML using Swing's HTMLEditorKit

    Hi all,
    I posted this question on the "Java programming", but I think I posted on the wrong forum. So, please let me know if I have posted on the wrong forum, again.
    Anyway, I have read an article on parsing HTML using the Swing HTML Parser (http://java.sun.com/products/jfc/tsc/articles/bookmarks/index.html). However, I find that the HTMLEditorKit is unable to understand the <Meta> tag under the <Head> tag? Is this true? I am getting an error message:
    javax.swing.text.ChangedCharSetException
    at javax.swing.text.html.parser.DocumentParser.handleEmptyTag(DocumentParser.java:172)
    at javax.swing.text.html.parser.Parser.startTag(Parser.java:327)
    at javax.swing.text.html.parser.Parser.parseTag(Parser.java:1786)
    at javax.swing.text.html.parser.Parser.parseContent(Parser.java:1821)
    at javax.swing.text.html.parser.Parser.parse(Parser.java:1980)
    at javax.swing.text.html.parser.DocumentParser.parse(DocumentParser.java:109)
    at javax.swing.text.html.parser.ParserDelegator.parse(ParserDelegator.java:74)
    at URLReader.main(URLReader.java:58)
    Below is a simple code to write out the html file it reads in:
    public static void main(String[] args) throws Exception {
    HTMLEditorKit.ParserCallback callback = new HTMLEditorKit.ParserCallback () {
    public void handleText(char[] data, int pos) {
    try {
    System.out.println(data);
    } catch (Exception e) {
    System.out.println("IOE: " + e);
    Reader reader = new FileReader("myFile.html");
    new ParserDelegator().parse(reader, callback, false);
    The html file that is having a problem reading in is:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>NWS WSR-88D Radar System Transmit/Receive Status</title>
    </head>
    <p>A <foo>xx</foo>link</html>
    If I take away <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">, there is no problem.
    Any suggestions? Thanks in advance.

    Hi,
    Setting the third argument really works!!! Yee..... haa....!!!
    WORKING SOLUTION: new ParserDelegator().parse(reader, callback, TRUE);
    MANY... MANY THANKS for looking at the problem!!!
    Send third argument in parse method as true.

  • Escaping of html characters

    ahoj!
    in an sql report i have to show text messages that include sometimes special html characters like <. is there an oracle function to convert this characters in the format & #60; (without the blank)? i don't want to replace all the special characters by myself.
    thanks!
    ciao,
    christian
    Edited by: Christian Ropposch on Apr 8, 2009 1:39 PM
    Edited by: Christian Ropposch on Apr 8, 2009 1:40 PM

    Hi Christian R.!
    If I understood right then you are using APEX. HTP and HTF are included with Oracle and APEX. You don't need the Application Server.
    regards

  • Parsing html files via an url

    Hi,
    I already have a Java program that is able to read in html files that are stored on my computers hard drive. Now I would like to expand its functionality by being able to parse html files straight from the web.
    For example, when the program is run, I would like to be able to give it an url for a given website. Then, I would like to be able to parse the html file that the link goes to.
    I've searched the forum, but have not been able to find anything of any real use. If you could offer an overview or point me towards a resource, I would be very greatful.

    If you've done things right, you have a HTML reader/parser that takes an InputStream. For Files, this would be a FileInputStream.
    For URLs, this would be the InputStream you get from URLConnection.getInputStream(). You can get a URLConnection by calling openConnection() on a URL instance (created from your input url of course).

  • Parsing HTML - best tool

    Hi guys, like to know the best open source API to parse HTML and get required data from it? Hopefully one thats uses SAX Parser but the HTML not fully XML compliant, i.e XHMTL
    Thanks
    Abe

    Thanks I found my anser to use Jericho HTML Parser. Any of you guys know of a better one?
    Thanks
    Abe

  • Error in parsing special characters

    We are getting the following error when we parse a clob with special characters like \226(- special dash char), \231(the "TM"-Trademark logo), \256(the "R"-Registered logo )
    "Invalid char in text" when the parser is loaded in the db.
    But when we tried to parse this xml file as a file input (parser is now outside the db), it parses the file fine and inserts the data into the db.
    We are wondering if there is a difference in parser parsing special characters between inside and outside the db.
    We are running xml parser version 2.0.2.7 and if this is a bug and being fixed in later version, is there a workaround for this version

    Please try our latest 9.0.1 version

  • HTML characters display incorrectly in Firefox

    Special HTML characters like ∠ and ← and ∝ (etc.) do not display as they should in Firefox on my Mac.
    I messed with Font Book earlier today, and though I don't think I made any fatal changes, I think that might have caused this problem (though certain symbols like ♥ had never shown up correctly).
    However, all of these characters display perfectly in Safari. See:
    http://www.plisher.org/safari.jpg
    http://www.plisher.org/firefox.jpg
    How might I fix this?

    Well, I just launched Firefox and opened this page in it. The angle, arrow and whatevertheotherthing is all displayed correctly, the heart however showed up in Firefox as a perpendicular line.
    I'm using Firefox 2.0.0.4, with the default font set to Geneva.
    Francine
    Francine
    Schwieder

  • Parsing HTML from Google API results

    Hello,
    I just downloaded the Google API (http://www.google.com/apis) and I am trying to parse the HTML content which is returned so that it can be displayed in a TextArea or some other GUI component.
    Here are my questions:
    1. Is there a Java class that can parse HTML and display it correctly?
    2. If not, are there are third party, prefabably free Java components that can do that?
    3. Has anyone tried out the Google API? Any interesting applications?
    Thank you.
    Hanxue

    To convert plain text to html, you can parse the text with a simple code like this
    1.
    String inputText = getInputText(); //
    StringBuffer HTMLOutputText = new StringBuffer();
    java.util.StringTokenizer st = new java.util.StringTokenzier(inputText, "\n\r");
    while ( st.hasMoreTokens() ) {
    HTMLOutputText.append(st.nextToken());
    HTMLOutputText.append("<br>");
    /// insert the top level HTML tags
    HTMLOutputText.insert(0, "<HTML> <HEAD><TITLE> Some Title</TITLE></HEAD> <BODY>");
    HTMLOutputText.insert( HTMLOutputText.getLength(), "</BODY> </HTML>" );
    2. even simpler, but as far as I know it doesn't display right in a JEditorPane
    String inputText = getInputText();
    inputText = "<HTML> <HEAD><TITLE> Some Title</TITLE></HEAD> <BODY> <PRE> <TT>" +
    + inputText + "</TT></PRE></BODY> </HTML>";

  • Could not parse html cannot be top level tag

    hi
    i have installed the siteminder webagent in sunone server, i am trying to call my first page (index.jsp) it contains the following jsp code
    <jsp:useBean id="helpBroker" class="javax.help.ServletHelpBroker" scope="session" />
    <%@ taglib uri="/WEB-INF/jhlib.tld" prefix="jh" %>
    <jh:validate helpBroker="<%= helpBroker %>" helpSetName="<%= strContext %>" />
    (here strContext is the delphi.hs file)
    then it's giving the "Could not parse html cannot be top level tag"
    error in servers log if i comment <jh:validate helpBroker="<%= helpBroker %>" helpSetName="<%= strContext %>" /> line then everything working fine
    can anybody suggest what is the error ?

    Could be your hs file inside your firewall configuration. Check whether you can able to assess hs with different session of your browser.

  • Parse HTML behaviour

    Hi,
    Can anybody explain the behavior of SunOne Web server when parse HTML is enabled for all html.
    If we have a valid html in the web server for instance http://servername/myhtml.html , then the page will be loaded by web server. The same case if we put anything in the URL after that then also the sma epage will get served. Consider an URL http://servername/myhtml.html/ahdjksad/asdhjsad/sdhjklsad/asjdksald (Anything after that HTML), web server would be able to load the myhtml.html page. If we disable the parse HTML , then it won�t work. I want a way to work the Server side includes where it shouldn�t server such wrong URLs.
    How this comes? If we look on the web server the path won�t be there on the server and it should give 404 error. Is it the way parse HTML works? Is there any way to restrict it by keeping the parse HTML functionality for server side includes enabled other than custom NSAPI?
    If anyone noticed this behavior please explain.
    Thanks,
    Rijesh.

    Hi,
    Acually i load one external html using LoadVars class
    methods.
    var oLoad:LoadVars=new LoadVars();
    oLoad.load("external.html");
    I want to parse that html in flash.
    I need some text from html page(html is having 200 line code)
    How can i parse that html and trace that particular text.

Maybe you are looking for

  • Unable to deploy BPEL Process --  Says If you have installed a patch to the

    I have a simple BPEL process that calles an ESB flow deployed in the same server. I am facing the following issue when trying to deploy the BPEL process. The ESB flow is deployed and is working perfectly. Configuration: JDeveloper 10.1.3.5 SOA Suite

  • Using Swing inside of CustomNode

    I have a CustomNode class and in the create() method I want some swing contents for a forum. But, I can't find a Node type that I can contain BorderPanel (Component). What Node can contain swing components? What is the best approach to mix swing insi

  • Calendars on iCloud

    When trying to set up calendars in iCloud it dies near the end with DAV config has stopped working.

  • Change name of parameters output by code module

    Hi guys, I am calling this  code module everytime but pass different parameters as input  & get different results I want to customize the  Parameter Name  for the output parameters as they get saved in my report. & so I can be very specific.

  • Asus USB-N13 is not working on Lion 10.7.2

    I purchased this device and used it on Lion 10.7.1, today I updated my MAC to Lion 10.7.2 And now I can not to connect to the Internet on this computer. I can open Asus utility and scan wi-fi networks, but I can not to connect to them. At the Asus we