1

I have a large and complex XML document in a file. I would like to do a text search of the XML document and have it give me the full XML path of the found text.

There is similar functionality in the RegEdit tool for Windows. If I search for "Clock Rate", I can see the full path in the status bar at the bottom of the window.

Regedit

I would like the same thing for XML files. For example, if I have

<foo><bar><baz>test</baz></bar></foo>

and I search for "test", I should see the full path foo\bar\baz

1 Answer 1

1

Sorry, I'm not familiar with any application (apart of Firebug - we'll return to that in a moment) that will do that out of the box. If it's really big - you might have to roll out a custom solution - it shouldn't be too complicated to use some XML/HTML parser and traverse through nodes until you find your wanted text - at which point you'd use some built in function to get XPath of the current element.

About the mentioned Firefox plugin - Firebug. I don't have enough reputation to show you a picture - but it's simple enough process to do this:

  • open XML file in Firefox - it probably doesn't have style defined, so you'll see the familiar:

    This XML file does not appear to have any style information associated with it. The document tree is shown below.

  • press F12 to call Firebug, you should see that the file is now rendered a bot like html - with every element being inline and thus the text is "merged" to a blob. That's of no import - the important part is that in the Firebug's HTML tab you can see your XML file parsed.

  • in Firebug's search input box type what you want to find, it'll be focused upon in the HTML tab.
  • right-click on the wanted element in the HTML tab, and at the top you should see two options of some interest:
    • Copy XPath
    • Copy CSS path

You can also try this with Firefox native Developer Tools:

  • open XML file in Firefox and press F12 twice to call and hide Firebug - I haven't found a way to make the file load up "correctly", instead of the tree view - Firebug does that.
  • open up Developer Tools (click "Open Menu" button -> Developer -> Toggle Tools)
  • click on your (poorly) rendered XML file to focus on it, Ctrl+F to search a phrase, it'll be highlighted just like with any other search
  • click on inspector tool ("Pick an element from the page") and click on the highlighted text
  • right-click on the found element in the Inspector tab, and choose "Copy Unique Selector" - though in my Firefox it'll sometimes return "undefined" instead of the top node, even though the XML in question does validate...

For a test file:

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
</catalog>

Where our searched phrase is "Computer", and thus out wanted node is:

<genre>Computer</genre>

The results are:

  • Firebug "Copy XPath"

/catalog/book/genre

  • Firebug "Copy CSS Path"

catalog book#bk101 genre

  • Firefox Developer Tools "Copy Unique Selector"

#bk101 > genre:nth-child(3)

My only concern is that you wrote your file is large. If it's really, really large - well, you know how browsers like to act from time to time. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.