Menu

How to replace angle brackets for custom tags ?

2023-04-05
2023-04-06
  • Nazarets Danylo

    Nazarets Danylo - 2023-04-05

    I have input some like this

    <p> Text for text, <custom>, <another/> 1>0 5<10 </p>
    

    But replace brackets only for "1>0 5<10" and create <custom> with close slash.</custom>

     

    Last edit: Nazarets Danylo 2023-04-05
  • Scott Wilson

    Scott Wilson - 2023-04-05

    Unless I'm missing something (Sourceforge does odd things with formatting) then I think thats a reasonable interpretation of the snippet. I presume what you get is something like this?

    <p> Text for text, <custom/>, <another/> 1&gt;0 5&lt;10 </p>

     
    • Nazarets Danylo

      Nazarets Danylo - 2023-04-06

      no, I got some like this.

      <p> Text for text, <custom><custom/>, <another><another/> 1&gt;0 5&lt;10 </p>
      

      But, I fixed this using next code:

        HtmlCleaner cleaner = new HtmlCleaner();
              CleanerProperties props = cleaner.getProperties();
              props.setTreatUnknownTagsAsContent(true);
              props.setNamespacesAware(false);
              props.setOmitHtmlEnvelope(true);
              props.setOmitXmlDeclaration(false);
              TagNode node = cleaner.clean(input);
              String result = new CompactHtmlSerializer(props).getAsString(node);
              return result.replaceAll("<\\?xml.*\\?>", "");
      

      As you can see, there is only one "not so good" thing here, and that is the xml tag

      "<?xml version="1.0" encoding="UTF-8"?>
      

      at the beginning of the line. I simply remove it with replaceAll. Perhaps there are configurations to prevent this tag from appearing as well, but I haven't found it)

       
  • Scott Wilson

    Scott Wilson - 2023-04-06

    The open and close tags, rather than self-closing tags, are how HTML is typically serialised.

    If you want to omit the XML tag, use props.setOmitXmlDeclaration(true);

     
    👍
    1

Log in to post a comment.

MongoDB Logo MongoDB