Skip to Content.
Sympa Menu

xom-interest - Re: [XOM-interest] Retrieving the path from root of an Element

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Benoît Thiébault <thiebault AT artenum.com>
  • To: xom-interest AT lists.ibiblio.org
  • Subject: Re: [XOM-interest] Retrieving the path from root of an Element
  • Date: Wed, 11 Jul 2012 13:34:55 +0200

Hi everyone,

I found a solution to my problem but it is quite ugly.
I would have prefered that function to be included in XOM... Isn't there another way?

private String buildElementPath(final Element element) {
final StringBuilder sb = new StringBuilder("/");

sb.append(element.getQualifiedName());
Node currentParentNode = element.getParent();
Element currentParentElement = null;
if (currentParentNode instanceof Element) {
currentParentElement = (Element) currentParentNode;
}

while (currentParentElement != null) {
sb.insert(0, currentParentElement.getQualifiedName());
sb.insert(0, "/");
currentParentNode = currentParentNode.getParent();
if (currentParentNode instanceof Element) {
currentParentElement = (Element) currentParentNode;
} else {
currentParentElement = null;
}
}

return sb.toString();
}

Le 11/07/2012 10:29, Benoît Thiébault a écrit :
Hi everyone,

I am new to XOM and so far, I managed to use it successfully.
There is however something that I would like to do and can't find a way
to do it.

I want to retrieve, as a String, the path to root of a given Element.
For instance, consider the following XML file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</book>
</bookstore>

When reading the Element associated with the <title> tag, I would like
to build the String: /bookstore/book/title.
I thought the following line would work :
element.getValue();

But this only returns a bunch of spaces (it seems to be the indentation
spaces of the XML file)...
What did I do wrong?

_______________________________________________
XOM-interest mailing list
XOM-interest AT lists.ibiblio.org
http://lists.ibiblio.org/mailman/listinfo/xom-interest





Archive powered by MHonArc 2.6.24.

Top of Page