-1
    <html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:epub="http://www.idpf.org/2007/ops" lang="en" 
    xmlns:ns="http://www.w3.org/2001/10/synthesis" 
    xmlns:m="http://www.w3.org/1998/Math/MathML" xml:lang="en">
    <head>
    </head>
    <body>
    <div id="1234">My name is ABC and my age is &lt; 30. </div>
    </body>
    </html>

I want a Java code that will replace the operators(only > and < )with the "lt" "gt"

  • 2
    This is not a correct HTML document. If you want to use the "<" character in the content, you should escape it as `<`. – vanje May 08 '18 at 09:36
  • "My friend", I suggest you check about [the list of special characters in XHTML](https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references#Entities_representing_special_characters_in_XHTML). – AxelH May 08 '18 at 10:34
  • And if you don't agree, use the [w3c validator](https://validator.w3.org/check) to see that even for XHTML : "_Line 8, Column 49: character "<" is the first character of a delimiter but occurred as data_" – AxelH May 08 '18 at 10:36
  • I agree.I want the java code to remove this dirt.Do you have a solution? – Vikas Deshmukh May 08 '18 at 10:42

4 Answers4

1

Please update as follows.

<div id="1234">
     My name is ABC and my age is &lt; 30. 
</div>
Chanaka Lakmal
  • 1,112
  • 9
  • 19
1

You need to replace reserved html characters such as '<' with what are known as HTML entities. In your case:

<div id="1234">
    My name is ABC and my age is &lt; 30. 
</div>

You can read more at https://www.w3schools.com/html/html_entities.asp which also gives you a table of characters with their replacements.

Ryan McCauley
  • 207
  • 1
  • 8
0

As others have said, this will be rejected by an XML parser because of the use of the unescaped < character. If you can't modify the content to contain &lt; character entity references instead (such as when you're receiving the content from an upstream source, or don't want to go through a large document to find and edit < characters manually), then I don't know a Java tool to help here.

But you can use an external SGML tool to "repair" your not-quite-XHTML (SGML is the more powerful markup meta-language from which XML is derived and of which XML is a subset). The osx program, part of the OpenSP package of SGML tools and specifically designed to convert SGML into XML, will auto-escape < characters when not used as markup delimiters. For example, what you have to do to repair your example snippet (without XML namespaces for simplicity) is to edit the file (or create a new one) so that it reads as follows:

<!DOCTYPE html [
  <!ELEMENT html - - (head,body)>
  <!ELEMENT head - - (#PCDATA)>
  <!ELEMENT body - - (div+)>
  <!ELEMENT div - - (#PCDATA)>
]>
<html>
  <head>
  </head>
  <body>
    <div>My name is ABC and my age is < 30. </div>
  </body>
</html>

Then your invoke the osx command line utility on the file, like in osx <yourfile>. The result output will contain a &lt; entity reference in place of an < character in content.

To install the OpenSP SGML tools on Ubuntu, use sudo apt-get install opensp, and similar on Mac OS and other OSs. See also http://www.jclark.com/sp/.

You don't actually need to edit your file or create a copy from it since you can also include your existing markup file into another one by using entity references, like so (where myxhtml.xml is the file containing the example data of your question):

<!DOCTYPE html [
  <!ELEMENT html - - (head,body)>
  <!ELEMENT head - - (#PCDATA)>
  <!ELEMENT body - - (div+)>
  <!ELEMENT div - - (#PCDATA)>
  <!ENTITY myxhtml SYSTEM "myxhtml.xml">
]>
&myxhtml;

Note that in practice it isn't quite as easy because osx will convert all element and attributes names to uppercase, and will reject XML namespace binding attributes since SGML's reference concrete syntax (default lexical rules) consider HTML the canonical form of html, and don't allow element and attribute names to have colon characters. To change that you need to put the SGML declaration for XML (the markup declaration beginning with <!SGML ... in the following code) at the begin of the file which tells SGML that the file uses XML naming rules among other things (you don't need to understand the details of the SGML declaration to make use of SGML here):

<!SGML "ISO 8879:1986 (WWW)"

     -- SGML Declaration for XML 1.0 --

     -- from: 
        Final text of revised Web SGML Adaptations Annex (TC2) to ISO 8879:1986
        ISO/IEC JTC1/SC34 N0029: 1998-12-06
        Annex L.2 (informative): SGML Declaration for XML

        changes made to accommodate validation are noted with 'VALID:'
     --

     CHARSET
         BASESET "ISO Registration Number 177//CHARSET
                 ISO/IEC 10646-1:1993 UCS-4 with implementation
                 level 3//ESC 2/5 2/15 4/6"
         DESCSET
                 0        9  UNUSED
                 9        2       9
                11        2  UNUSED
                13        1      13
                14       18  UNUSED
                32       95      32
               127        1  UNUSED
               128       32  UNUSED
               160    55136     160
             55296     2048  UNUSED  -- surrogates --
             57344     8190   57344
             65534        2  UNUSED  -- FFFE and FFFF --
             65536  1048576   65536

     CAPACITY NONE  -- Capacities are not restricted in XML --

     SCOPE DOCUMENT

     SYNTAX
         SHUNCHAR NONE
         BASESET "ISO Registration Number 177//CHARSET
                 ISO/IEC 10646-1:1993 UCS-4 with implementation
                 level 3//ESC 2/5 2/15 4/6"
         DESCSET
             0 1114112 0
         FUNCTION
             RE    13
             RS    10
             SPACE 32
             TAB   SEPCHAR 9
         NAMING
             LCNMSTRT ""
             UCNMSTRT ""
             NAMESTRT
                 58 95 192-214 216-246 248-305 308-318 321-328
                 330-382 384-451 461-496 500-501 506-535 592-680
                 699-705 902 904-906 908 910-929 931-974 976-982
                 986 988 990 992 994-1011 1025-1036 1038-1103
                 1105-1116 1118-1153 1168-1220 1223-1224
                 1227-1228 1232-1259 1262-1269 1272-1273
                 1329-1366 1369 1377-1414 1488-1514 1520-1522
                 1569-1594 1601-1610 1649-1719 1722-1726
                 1728-1742 1744-1747 1749 1765-1766 2309-2361
                 2365 2392-2401 2437-2444 2447-2448 2451-2472
                 2474-2480 2482 2486-2489 2524-2525 2527-2529
                 2544-2545 2565-2570 2575-2576 2579-2600
                 2602-2608 2610-2611 2613-2614 2616-2617
                 2649-2652 2654 2674-2676 2693-2699 2701
                 2703-2705 2707-2728 2730-2736 2738-2739
                 2741-2745 2749 2784 2821-2828 2831-2832
                 2835-2856 2858-2864 2866-2867 2870-2873 2877
                 2908-2909 2911-2913 2949-2954 2958-2960
                 2962-2965 2969-2970 2972 2974-2975 2979-2980
                 2984-2986 2990-2997 2999-3001 3077-3084
                 3086-3088 3090-3112 3114-3123 3125-3129
                 3168-3169 3205-3212 3214-3216 3218-3240
                 3242-3251 3253-3257 3294 3296-3297 3333-3340
                 3342-3344 3346-3368 3370-3385 3424-3425
                 3585-3630 3632 3634-3635 3648-3653 3713-3714
                 3716 3719-3720 3722 3725 3732-3735 3737-3743
                 3745-3747 3749 3751 3754-3755 3757-3758 3760
                 3762-3763 3773 3776-3780 3904-3911 3913-3945
                 4256-4293 4304-4342 4352 4354-4355 4357-4359
                 4361 4363-4364 4366-4370 4412 4414 4416 4428
                 4430 4432 4436-4437 4441 4447-4449 4451 4453
                 4455 4457 4461-4462 4466-4467 4469 4510 4520
                 4523 4526-4527 4535-4536 4538 4540-4546 4587
                 4592 4601 7680-7835 7840-7929 7936-7957
                 7960-7965 7968-8005 8008-8013 8016-8023 8025
                 8027 8029 8031-8061 8064-8116 8118-8124 8126
                 8130-8132 8134-8140 8144-8147 8150-8155
                 8160-8172 8178-8180 8182-8188 8486 8490-8491
                 8494 8576-8578 12295 12321-12329 12353-12436
                 12449-12538 12549-12588 19968-40869 44032-55203

             LCNMCHAR ""
             UCNMCHAR ""
             NAMECHAR
                 45-46 183 720-721 768-837 864-865 903 1155-1158
                 1425-1441 1443-1465 1467-1469 1471 1473-1474
                 1476 1600 1611-1618 1632-1641 1648 1750-1764
                 1767-1768 1770-1773 1776-1785 2305-2307 2364
                 2366-2381 2385-2388 2402-2403 2406-2415
                 2433-2435 2492 2494-2500 2503-2504 2507-2509
                 2519 2530-2531 2534-2543 2562 2620 2622-2626
                 2631-2632 2635-2637 2662-2673 2689-2691 2748
                 2750-2757 2759-2761 2763-2765 2790-2799
                 2817-2819 2876 2878-2883 2887-2888 2891-2893
                 2902-2903 2918-2927 2946-2947 3006-3010
                 3014-3016 3018-3021 3031 3047-3055 3073-3075
                 3134-3140 3142-3144 3146-3149 3157-3158
                 3174-3183 3202-3203 3262-3268 3270-3272
                 3274-3277 3285-3286 3302-3311 3330-3331
                 3390-3395 3398-3400 3402-3405 3415 3430-3439
                 3633 3636-3642 3654-3662 3664-3673 3761
                 3764-3769 3771-3772 3782 3784-3789 3792-3801
                 3864-3865 3872-3881 3893 3895 3897 3902-3903
                 3953-3972 3974-3979 3984-3989 3991 3993-4013
                 4017-4023 4025 8400-8412 8417 12293 12330-12335
                 12337-12341 12441-12442 12445-12446 12540-12542

             NAMECASE
                 GENERAL NO
                 ENTITY  NO
         DELIM
             GENERAL  SGMLREF
             HCRO     "&#38;#x"
                      -- Ampersand followed by "#x" (without quotes) --
             NESTC    "/"
             NET      ">"
             PIC      "?>"
             SHORTREF NONE

         NAMES
             SGMLREF

         QUANTITY
             NONE -- Quantities are not restricted in XML --

         ENTITIES
             "amp"  38
             "lt"   60
             "gt"   62
             "quot" 34
             "apos" 39

     FEATURES
         MINIMIZE
             DATATAG NO
             OMITTAG NO
             RANK    NO
             SHORTTAG
                 STARTTAG
                     EMPTY    NO
                     UNCLOSED NO
                     NETENABL IMMEDNET
                 ENDTAG
                     EMPTY    NO
                     UNCLOSED NO
                 ATTRIB
                     DEFAULT  YES
                     OMITNAME NO
                     VALUE    NO
             EMPTYNRM  YES
             IMPLYDEF
                 ATTLIST  YES
                 DOCTYPE  NO
                 ELEMENT  YES
                 ENTITY   NO
                 NOTATION YES
         LINK
             SIMPLE   NO
             IMPLICIT NO
             EXPLICIT NO
         OTHER
             CONCUR   NO
             SUBDOC   NO
             FORMAL   NO
             URN      NO
             KEEPRSRE YES
             VALIDITY NOASSERT
             ENTITIES
                 REF      ANY
                 INTEGRAL YES

     APPINFO NONE

     SEEALSO "ISO 8879//NOTATION Extensible Markup Language (XML) 1.0//EN"
>
<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:epub="http://www.idpf.org/2007/ops" lang="en" 
    xmlns:ns="http://www.w3.org/2001/10/synthesis" 
    xmlns:m="http://www.w3.org/1998/Math/MathML" xml:lang="en">
    <head>
    </head>
    <body>
    <div id="1234">My name is ABC and my age is < 30. </div>
    </body>
</html>

Note this example also doesn't have a <!DOCTYPE ... declaration. This is because the SGML declaration for XML will make the SGML parser accept undeclared elements according to IMPLYDEF ELEMENT YES, and will also deal with XML-style empty elements (eg. <img/>).

imhotap
  • 2,275
  • 1
  • 8
  • 16
  • Thanks man.Lets say if i have a html containing "lt" and "gt" rather than the symbols.So can I convert it to the xhtml using any tool? – Vikas Deshmukh May 09 '18 at 05:33
  • @Vikas Deshmukh If you want those tokens to be replaced in a context-sensitive way using SGML, you could short reference delimiters (`SHORTREF`); I've explained the technique in another answer eg. – imhotap May 09 '18 at 06:25
0

Use HTML cleaner to resolve this issue.Try to use the latest version of it.

 //initial = [load your HTML here]
    initial = initial.replaceAll("&lt;", "<");
    initial = initial.replaceAll("&gt;", ">");
    TagNode node = cleaner.clean(initial);
    String cleaned = new SimpleHtmlSerializer(cleaner.getProperties()).getAsString(node);
    System.out.println(cleaned);

Refer this link for more details