Built-in HTML & CSS Parsers
Parse, analyze, and transform HTML and CSS programmatically with Oorian's native parsers.
Parse and Transform
Work with HTML and CSS as structured data, not just strings.
HTML Parsing
Parse HTML strings into DOM-like structures. Navigate, query, and modify elements.
CSS Parsing
Parse CSS stylesheets. Access rules, selectors, and declarations programmatically.
Transformation
Modify parsed content and serialize back to strings.
Query Support
Find elements by tag, class, ID, or CSS selectors.
Validation
Validate HTML structure and CSS syntax.
Cleanup
Sanitize HTML, remove dangerous content, normalize whitespace.
HTML Parser
Parse HTML strings into navigable element structures.
// Parse HTML content
String html = "<div class='container'>" +
"<h1 id='title'>Hello World</h1>" +
"<p class='intro'>Welcome to our site.</p>" +
"<ul><li>Item 1</li><li>Item 2</li></ul>" +
"</div>";
HtmlDocument doc = HtmlParser.parse(html);
// Navigate the structure
HtmlElement root = doc.getRootElement();
HtmlElement titleEl = doc.getElementById("title");
List<HtmlElement> paragraphs = doc.getElementsByTagName("p");
List<HtmlElement> intros = doc.getElementsByClassName("intro");
// Query with CSS selectors
List<HtmlElement> listItems = doc.querySelectorAll("ul li");
HtmlElement firstItem = doc.querySelector("ul li:first-child");
// Modify elements
titleEl.setText("Welcome!");
titleEl.setAttribute("class", "main-title");
// Add new elements
HtmlElement newPara = new HtmlElement("p");
newPara.setText("This is a new paragraph.");
root.appendChild(newPara);
// Serialize back to string
String modifiedHtml = doc.toHtml();
String prettyHtml = doc.toPrettyHtml();CSS Parser
Parse and manipulate CSS stylesheets programmatically.
// Parse CSS content
String css = ".container { max-width: 1200px; margin: 0 auto; }" +
".title { font-size: 24px; color: #333; }" +
"@media (max-width: 768px) { .container { padding: 20px; } }";
CssStyleSheet sheet = CssParser.parse(css);
// Access rules
List<CssRule> rules = sheet.getRules();
for (CssRule rule : rules)
{
String selector = rule.getSelector();
Map<String, String> declarations = rule.getDeclarations();
System.out.println("Selector: " + selector);
for (Map.Entry<String, String> decl : declarations.entrySet())
{
System.out.println(" " + decl.getKey() + ": " + decl.getValue());
}
}
// Find specific rules
CssRule containerRule = sheet.getRuleBySelector(".container");
String maxWidth = containerRule.getDeclaration("max-width");
// Modify rules
containerRule.setDeclaration("max-width", "1400px");
containerRule.setDeclaration("padding", "0 20px");
// Add new rules
CssRule newRule = new CssRule(".footer");
newRule.setDeclaration("background-color", "#1e293b");
newRule.setDeclaration("color", "#ffffff");
sheet.addRule(newRule);
// Serialize back to string
String modifiedCss = sheet.toCss();
String minifiedCss = sheet.toMinifiedCss();Use Cases
Content Sanitization
Parse user-submitted HTML and remove potentially dangerous elements like scripts and event handlers.
Email Templates
Parse HTML email templates, replace placeholders, and inline CSS for email client compatibility.
Web Scraping
Parse HTML from external sources and extract specific data or content.
Theme Customization
Parse CSS themes, allow users to customize colors and fonts, regenerate the stylesheet.
HTML Migration
Parse legacy HTML content and transform it to match new structural requirements.
CSS Optimization
Parse stylesheets, remove unused rules, combine duplicates, and minify output.