Dear All,
I have one requirement that, I have one xml testfile called testfile.xml , as shown in below.
Now , I am calling this firstname , lastname and number inside my servlet, by using below JAVA code.
ISSUE - I am not able to fetch the lastname value from the XML file.
logger.info("LastName INSIDE SEARCH ====== " + lastName);
Below is my xml file.
************************** testfile.xml ************************
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
Hello @SunitaCh
You would first need to read the XML data from the binary
InputStream xmlContent = fileNode.getProperty(JcrConstants.JCR_DATA).getBinary().getStream();
Reference: https://techrevel.blog/2020/09/19/editing-jcrdata-binary-in-aem/
One the InputStream is available, XML libaries can be used to read the data
// create a new DocumentBuilderFactory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
// use the factory to create a documentbuilder
DocumentBuilder builder = factory.newDocumentBuilder();
// create a new document from input stream
FileInputStream fis = new FileInputStream("Student.xml");
Document doc = builder.parse(fis);
// get the first element
Element element = doc.getDocumentElement();
// get all child nodes
NodeList nodes = element.getChildNodes();
// print the text content of each child
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println("" + nodes.item(i).getTextContent());
}
} catch (Exception ex) {
ex.printStackTrace();
}
@SunitaCh Did you find the suggestion from Aanchal helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.
Views
Replies
Total Likes
@aanchal-sikka and @kautuk_sahni ,
Now all are coming fine, but when I am trying to get only LastName by using the below code highlighted in red text then I am getting error
Views
Likes
Replies
Views
Likes
Replies