Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Convert HTML symbols to equivalent Unicode

by dorward (Curate)
on Apr 14, 2009 at 11:35 UTC ( #757374=note: print w/replies, xml ) Need Help??

in reply to Convert HTML symbols to equivalent Unicode

Your question is very confusing.

I need to parse some HTML files and have to write a XML output file. In some cases I get a XML parser error.

  • What are you using to parse the HTML?
  • What are you using to write the XML?
  • What "XML parser error" do you get?
  • Show us some code

The symbol ( REGISTERED SIGN ) need to be convert to its equivalent unicode U00AE

Is "" the result you get after parsing the HTML? i.e. the character and not an ampersand followed by an identifier and then a semi-colon? If so, then the parsing is working fine, and it is the output you need to worry about. (I mention this because PerlMonks takes HTML input, so you might have wanted to say ® rather then ®.)

Any XML library should be outputting something appropriate when given as input. Either it will output an entity (which should be absolutely fine, since you should be parsing XML only with an XML parser which can handle such things) or it will output the character in whatever character encoding is being used (so you just need to make sure you are outputting UTF-8 (or whichever unicode encoding you want) — how you do that depends on which XML library you are using.

I don't want Decimal Equivalent or HTML entities

Named HTML entities could screw things up in XML, but the decimal entity should not.

as this XML file should be parsed in JSON.

This doesn't make sense. XML is a data format. JSON is a completely different data format.

You can't parse anything in JSON.

You could store an XML document as a string inside a JSON object, but that shouldn't prevent you from using entities — you would parse the JSON to extract the string of XML, then put that string in an XML parser to extract the data from it.

What is the problem you are really trying to solve? You don't seem to have provided enough detail here.

  • Comment on Re: Convert HTML symbols to equivalent Unicode

Replies are listed 'Best First'.
Re^2: Convert HTML symbols to equivalent Unicode
by jai_dgl (Beadle) on Apr 14, 2009 at 12:38 UTC
    Thanks for your reply,
    I spider some web pages and pull some information from them.

    Then I use HTML::Entities::encode_entities() function to convert the special characters

    when is passed to it, it is converted to ®
    When HTML::Entities::encode_numeric() is used it gives ®

    My final idea is to write a XML file with its Unicode equivalent(U+00AE).


      I spider some web pages and pull some information from them.

      At this point you will either have the results of parsing HTML or you will have raw HTML.

      when is passed to it, it is converted to ®

      This suggests you are dealing with parsed HTML, with any entities already converted to real characters.

      In this case, there are two approaches you could take.

      1. Use a module designed for writing XML
      2. Use a generic template module

      If you use a module designed for writing XML then you can just pass the and the module will output either a raw or a numeric entity for it. Any XML parser that this XML gets read by should be able to cope with either, so it doesn't matter which you end up with and you don't need to worry about it.

      If you use a generic templating language, then you have to deal with using a raw or converting it with HTML::Entities::encode_numeric() or a similar module. Basically — you have to do all the things that a proper XML module would do for you.

      It sounds like you are using a generic templating language, but it is almost certainly better to use a real XML module.

      Afterthought: You might also be messing about with raw strings in the middle of your code. That way lies madness.

      Hello, I wants to numeric entities but below code will display à i need ex: © how can i get this output. my $encoded = HTML::Entities::encode_numeric($val, '^\n\x20-\x25\x27-\x7e'); Thanks, Umesh

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://757374]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2022-10-05 09:51 GMT
Find Nodes?
    Voting Booth?
    My preferred way to holiday/vacation is:

    Results (22 votes). Check out past polls.