0

I have a xml as follows need to store the values of xml field in the array list can anybody help.

<Search>
<Fields>
<Field>
<IndexId>241</IndexId><IndexName>ProductCode</IndexName><IndexType>S</IndexType><IndexLength>100</IndexLength><IndexFlag>D</IndexFlag><UsefulInfoFlag>N</UsefulInfoFlag><UsefulInfosize>255</UsefulInfosize><IndexAttribute>0</IndexAttribute></Field>
<Field>
<IndexId>263</IndexId><IndexName>Partner Name</IndexName><IndexType>S</IndexType><IndexLength>100</IndexLength><IndexFlag>D</IndexFlag><UsefulInfoFlag>N</UsefulInfoFlag><UsefulInfosize>255</UsefulInfosize><IndexAttribute>0</IndexAttribute></Field>
<Field>
<IndexId>420</IndexId><IndexName>Day</IndexName><IndexType>S</IndexType><IndexLength>100</IndexLength><IndexFlag>D</IndexFlag><UsefulInfoFlag>N</UsefulInfoFlag><UsefulInfosize>255</UsefulInfosize><IndexAttribute>0</IndexAttribute></Field>
<Field>
<IndexId>421</IndexId><IndexName>Month</IndexName><IndexType>S</IndexType><IndexLength>100</IndexLength><IndexFlag>D</IndexFlag><UsefulInfoFlag>N</UsefulInfoFlag><UsefulInfosize>255</UsefulInfosize><IndexAttribute>0</IndexAttribute></Field>
</Fields>
</Search>
1
  • Use JAX-B or JAX-P create a class that resembles your xml. Commented Nov 2, 2020 at 5:28

1 Answer 1

1

There is various Parser library you can use. reference link: https://www.tutorialspoint.com/java_xml/java_xml_parsers.htm

Here you go with one for them

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;


File inputFile = new File("input.txt");

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();

NodeList nList = doc.getElementsByTagName("Field");

for (int temp = 0; temp < nList.getLength(); temp++) {
    System.out.println(eElement.getAttribute("IndexId"));
    System.out.println(eElement.getAttribute("IndexName"));
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.