i´m quite new to programming and especially new to XML parsing and i´m currently having issues with xml-parsing in java. I´ve tried several things to get specific information from a xml-file and writing it into an object, which later on will be put into an arraylist and sorted. Here´sa short cut from my XML-file (i changed names and values as i´m not sure if i´m allowed to share it.):
<things>
<code V="123"/>
<region V="38"/>
<hl_list>
<hl>
<stuff_hmv V="XXX" DN="some other stuff"/>
<secondary_code V="Z98"/>
<infotext V="max 12 units"/>
<kl_list>
<kl V="I" DN="some other stuff">
<diag_list>
<diag V="WS1" DN="illness 1"/>
<diag V="WS2" DN="illness 2"/>
<diag V="EX1" DN="illness 3"/>
</diag_list>
</kl>
</kl_list>
</hl>
</hl_list>
</things>
<things>
<code V="456"/>
<region V="45"/>
<hl_list>
<hl>
<stuff_hmv V="X1X" DN="some other stuff"/>
<infotext V="max 13 units"/>
<kl_list>
<kl V="II" DN="some other stuff">
<diag_list>
<diag V="WS1" DN="illness 3"/>
<diag V="WS2" DN="illness 2"/>
<diag V="EX1" DN="illness 4"/>
</diag_list>
</kl>
</kl_list>
</hl>
</hl_list>
</things>
This is basically what i´m working with. I have several "thing-blocks" and i need the following information (if existing) in my objects. If the object is filled it should be put into an arraylist and the next block continues. Information from xml:
so far i´ve tried to get the information with getElementsByTagName and getters/setters for my object.
JAXB could be the solution for you
@XmlRootElement
public class Things {
private String code;
private String region;
@XmlElement
public void setCode(String code) {
this.code = code;
}
@XmlElement
public void setCode(String region) {
this.region = region;
}
}
JAXBContext jaxbContext = JAXBContext.newInstance(Things.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Customer customer = (Customer) jaxbUnmarshaller.unmarshal(new File("C:\\file.xml"));
for more information check jaxb and how to parse xml to java object?
File fXmlFile = new File("/Users/mkyong/staff.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
//optional, but recommended
//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("staff");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Staff id : " + eElement.getAttribute("id"));
System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());
}
}
SIMPLY REPLACE THE STAFF MARKEUP TAG WITH CODE OR THINGS WHICH ONENODE YOU WANT TO PICKUP
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.