-1
String s= "<tr><td><b>ErrorCode</b></td><td>myName</td></tr><tr><td><b>";      
String p[]= s.split(`enter code here`);
Ivar
  • 6,138
  • 12
  • 49
  • 61
  • is the string is guaranteed to be same, After Errorcode you will get your myName? – Rishal Aug 04 '22 at 11:25
  • 1
    Does this answer your question? [How can I efficiently parse HTML with Java?](https://stackoverflow.com/questions/2168610/how-can-i-efficiently-parse-html-with-java) – jsheeran Aug 04 '22 at 11:26
  • [Do not use regular expressions to parse HTML.](https://stackoverflow.com/questions/701166/can-you-provide-some-examples-of-why-it-is-hard-to-parse-xml-and-html-with-a-reg) Doing so *will* break eventually. – VGR Aug 04 '22 at 16:22

1 Answers1

1

As per your string it looks like HTML and if you want to parse it, there are multiple ways to do it. As in comments one of the ways suggested is JSOUP.

If you are aware of the tags and path you can use XPath to obtain the information you need.

For reference example as below:

    String s = "<tr><td><b>ErrorCode</b></td><td>myName</td></tr>";

    InputSource inputXML = new InputSource(new StringReader(s));
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();

    String xpathExpression = "/tr/td[2]"; //Xpath to evaluate and find your value as per your String.

    XPathExpression expr = xpath.compile(xpathExpression);
    
   //It will give you List of Nodes in which you can iterate and find out your value.
    NodeList nodes = (NodeList) expr.evaluate(inputXML, XPathConstants.NODESET);
    System.out.println(nodes.item(0).getTextContent());

Another approach is to use Html Parsing Libraries as given in suggestions. JSoup

For Jsoup (XML) below is reference code can be used.

    String s = "<tr><td><b>ErrorCode</b></td><td>myName</td></tr>";
    Document doc = Jsoup.parse(s,"", Parser.xmlParser());
    Elements td = doc.select("td");
    System.out.println(td.get(1).text());

For Jsoup(Html) your tags need to be proper.

    String s = "<table><tr><td><b>ErrorCode</b></td><td>myName</td></tr></table>";
    Document doc = Jsoup.parse(s);
    Elements td = doc.select("td");
    System.out.println(td.get(1).text());
Rishal
  • 1,480
  • 1
  • 11
  • 19