Hi this is sri and i am new to java.Here is a xml file.
My task is to parse all the xml elements in java without using any parser like dom,sax.
So pls help me how to parse xml elements without parsers.I have to get same out put as when i use parsers.

<?xml version="1.0" ?>

<Company>

     <Project>

       <title>Customersupport</title>
       
       <frontend>php</frontend>
       
       <backend>mysql</backend>
       
       <client>Matrix</client>
       
       <team_size>20</team_size>
     
     </Project>


     <Project>
     
       <title>Enterprise</title>
       
       <frontend>php</frontend>
       
       <backend>mysql</backend>
       
       <client>Sudha</client>
       
       <team_size>5</team_size>
     
     </Project>

</Company>

Thanq

Dani AI

Generated

As pointed out, asking to parse XML “without a parser” is unusual because XML has many corner cases that real parsers handle. Given ’s constraint to use only string functions/regular expressions, a safe approach is: accept this only for very small, strictly controlled XML (no attributes, no namespaces, no CDATA, no entity refs, no mixed content, no nested elements with the same name). For anything else, a proper XML parser is the correct choice.

A minimal, practical helper that extracts element text by tag name (multiline-aware, non-greedy) looks like this:

import java.util.regex.*;
import java.util.*;

public static List<String> extract(String xml, String tag) {
    List<String> out = new ArrayList<>();
    Pattern p = Pattern.compile("(?s)<" + Pattern.quote(tag) + ">(.*?)</" + Pattern.quote(tag) + ">");
    Matcher m = p.matcher(xml);
    while (m.find()) {
        out.add(m.group(1).trim());
    }
    return out;
}

Notes and troubleshooting tips: this will return inner text (including any nested tags) for each matching pair. To accept attributes on the start tag, change the pattern to use "<" + Pattern.quote(tag) + "\\b[^>]*>(.*?)</" + Pattern.quote(tag) + ">". The main failure modes are nested identical tags (regex cannot enforce hierarchical nesting), encoded entities (e.g., &amp;), CDATA blocks, comments, self-closing tags, and differing encodings—each can break a simple string-based extractor. Test with edge cases (attributes, nested elements, CDATA, empty elements) and only use this technique where the XML shape is fixed and trusted; otherwise switch to a proper parser (SAX/StAX/DOM) for correct, maintainable handling.

Recommended Answers

All 2 Replies

Well that's confusing....the entire point of parsing an XML document, is to use a parser. So what is it that you're really trying to do? There are other parsers out there aside from DOM or Sax, but this question doesn't make sense :P

Well that's confusing....the entire point of parsing an XML document, is to use a parser. So what is it that you're really trying to do? There are other parsers out there aside from DOM or Sax, but this question doesn't make sense :P

Sorry to making you confuse.I should read the all elements without use of any parser.I can make use of string functions and also regular expressions.So is there any way to do like that.If so give me some code.Pls

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.