Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3791514
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:28:53+00:00 2026-05-19T12:28:53+00:00

I’m looking for code which can do the following. Given a snippet of XML,

  • 0

I’m looking for code which can do the following. Given a snippet of XML, say:

<c>Some text</c>

and a complete XML file:

<a>
   <b>
      <c>Some text</c>
   </b>
</a>

check that the snippet is indeed a valid subset of the complete XML file. I’ve been looking at XMLUnit which looks really good, but it seems to only validate complete files against each other.

For the sample above, a simple string comparison would work, but other features I would like to support might be:

1) Order of child elements not important. E.g.

<b>
   <c>Some text</c>
   <d>Other text</d>
</b>

is a valid subset of

<a>
   <b>
      <d>Other text</d>
      <c>Some text</c>
   </b>
</a>

2) Ignore whitespace, tabs, new lines etc.
3) A nice to have would be XMLUnit’s skeleton feature where node names and structure are checked, but not content values. E.g. <c>Some text</c> would be valid when compared against <c>Other text</c>.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T12:28:54+00:00Added an answer on May 19, 2026 at 12:28 pm

    I don’t think there’s such a library but you could solve it in the following way (if your xml fragment starts with an element). I assume you are familiar with XMLUnit.

    public boolean isSubset(Document document, Element element) {
        NodeList list = document.getElementsByTagName(element.getNodeName());
        for (int i = 0; i < list.getLength(); i++) {
            Element el = (Element) list.item(i);
            Document clone = toNewDocument(el);
            //compare element with clone with XMLUnit
            //...
            if (equal) {
                return true;
            }
        }
        return false;
    }
    
    private Document toNewDocument(Element el) {
        // createing a new DOM-document...
        Document document = documentBuilder.newDocument();
        Node node = document.importNode(el, true);
        document.getDocumentElement().appendChild(node);
        return document;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
In my XML file chapters tag has more chapter tag.i need to display chapters
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from
I have a reasonable size flat file database of text documents mostly saved in
I'm parsing an XML file, the creators of it stuck in a bunch social
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.