Reputation: 75
My XML file is structured like so:
<parent xml:space="preserve">
Hello, my name is
<variable type="firstname">ABC</variable>
and my last name is
<variable type="lastname">XYZ</variable>
</parent>
I need a way to get the text output in this format:
"Hello, my name is ABC and my last name is XYZ".
Now the issue with using jdom2 is that element.getText() method returns the entire string as a single string (with no regard to position of the child tags):
"Hello, my name is and my last name is".
Is there anyway I can get the position of the child tags/delimit them, so that even a manual variable insert can be done at some later point?
Upvotes: 3
Views: 1728
Reputation: 22963
edit The example uses the Xerces parser which is included in Java runtime API for the DOM. For a JDOM2 solution see the answer from rolfl.
As a starting point you could use following snippet. Based on what you really want to achieve changes needs to be done by yourself.
xml = "<parent xml:space=\"preserve\">\n"
+ "Hello, my name is\n"
+ " <variable type=\"firstname\">ABC</variable>\n"
+ "and my last name is \n"
+ " <variable type=\"lastname\">XYZ</variable>\n"
+ "</parent>";
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = builder.parse(new ByteArrayInputStream(xml.getBytes()));
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodeList = (NodeList) xPath.compile("//parent").evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println(nodeList.item(i).getTextContent());
}
output
Hello, my name is
ABC
and my last name is
XYZ
note The snippet is not optimised. See it more as a PoC.
Upvotes: 1
Reputation: 17707
getText is specified in JDOM to return the immediate Text content of the element. JDOM also has the method getValue()
which returns:
Returns the XPath 1.0 string value of this element, which is the complete, ordered content of all text node descendants of this element (i.e. the text that's left after all references are resolved and all other markup is stripped out.)
Applying this to your document:
Document doc = new SAXBuilder().build("parentwtext.xml");
Element root = doc.getRootElement();
System.out.println(root.getValue());
I get the output (there's an empty line at the beginning I can't show in here):
Hello, my name is
ABC
and my last name is
XYZ
Upvotes: 0