Lets Look in this API.
Steps:
1) First create a Java Project and include the Jar xom-1.2.8.jar in class path.
2)Create two Java Src file as below. API Used in RED Line.
First Java source in:com.myapp
package com.myapp;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import nu.xom.Attribute;
import nu.xom.Document;
import nu.xom.Element;
public class HelloWorldXomXmlTest {
List<PcFxStComPartNumberInfo> gridData;
public List<PcFxStComPartNumberInfo> getGridData() {
return gridData;
}
public void setGridData(List<PcFxStComPartNumberInfo> gridData) {
this.gridData = gridData;
}
public static void main(String[] args) {
PcFxStComPartNumberInfo info=new PcFxStComPartNumberInfo();
info.setDescription("my description");
info.setPartCode("0111");
info.setPartNum("TIPS");
info.setStatus("Alive");
PcFxStComPartNumberInfo info2=new PcFxStComPartNumberInfo();
info2.setDescription("my description2");
info2.setPartCode("0122");
info2.setPartNum("MEMS");
info2.setStatus("DEAD");
List<PcFxStComPartNumberInfo> list=new ArrayList<PcFxStComPartNumberInfo>();
list.add(info);
list.add(info2);
HelloWorldXomXmlTest test=new HelloWorldXomXmlTest();
test.setGridData(list);
System.out.println(test.getXMLForPartNumber("hello"));
}
public String getXMLForPartNumber(String userSearchedKeyword){
int totalRecord=this.gridData.size();
Element root = new Element("search_result");
Attribute searchKeyword = new Attribute("searchkeyword", userSearchedKeyword);
Attribute records = new Attribute("records", String.valueOf(totalRecord));
root.addAttribute(searchKeyword);
root.addAttribute(records);
Element part_numbers = new Element("part_numbers");
for (PcFxStComPartNumberInfo info : this.gridData) {
String partNumberValue = info.getPartNum();
Element part_number = new Element("part_number");
Attribute name_attribute = new Attribute("name", partNumberValue);
part_number.addAttribute(name_attribute);
Element description = new Element("description");
String descriptionValue = info.getDescription();
description.appendChild(descriptionValue);
part_number.appendChild(description);
part_numbers.appendChild(part_number);
}
root.appendChild(part_numbers);
Document doc = new Document(root);
String result = doc.toXML();
return result;
}
}
Second Java source in: com.myapp packge. You can remove comparable if you want.
package com.myapp;
import java.util.*;
public class PcFxStComPartNumberInfo implements Comparable<PcFxStComPartNumberInfo>{
private String partNum;
private String partCode;
//private String subClassId;
//private String subClassName;
private String description;
private String status;
//private List<PcFxStComOrderablePartNumber> opnList;
public String getPartNum() {
return partNum;
}
public void setPartNum(String partNum) {
this.partNum = partNum;
}
public String getPartCode() {
return partCode;
}
public void setPartCode(String partCode) {
this.partCode = partCode;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public int compareTo(PcFxStComPartNumberInfo o) {
// TODO Auto-generated method stub
if( o==null)
return 1;
else
return this.getPartNum().compareTo(o.getPartNum());
}
}
Step3) Run your first Java Source a Java Application and see the result. Its better than DOM, SAX and StAX API.
Steps:
1) First create a Java Project and include the Jar xom-1.2.8.jar in class path.
2)Create two Java Src file as below. API Used in RED Line.
First Java source in:com.myapp
package com.myapp;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import nu.xom.Attribute;
import nu.xom.Document;
import nu.xom.Element;
public class HelloWorldXomXmlTest {
List<PcFxStComPartNumberInfo> gridData;
public List<PcFxStComPartNumberInfo> getGridData() {
return gridData;
}
public void setGridData(List<PcFxStComPartNumberInfo> gridData) {
this.gridData = gridData;
}
public static void main(String[] args) {
PcFxStComPartNumberInfo info=new PcFxStComPartNumberInfo();
info.setDescription("my description");
info.setPartCode("0111");
info.setPartNum("TIPS");
info.setStatus("Alive");
PcFxStComPartNumberInfo info2=new PcFxStComPartNumberInfo();
info2.setDescription("my description2");
info2.setPartCode("0122");
info2.setPartNum("MEMS");
info2.setStatus("DEAD");
List<PcFxStComPartNumberInfo> list=new ArrayList<PcFxStComPartNumberInfo>();
list.add(info);
list.add(info2);
HelloWorldXomXmlTest test=new HelloWorldXomXmlTest();
test.setGridData(list);
System.out.println(test.getXMLForPartNumber("hello"));
}
public String getXMLForPartNumber(String userSearchedKeyword){
int totalRecord=this.gridData.size();
Element root = new Element("search_result");
Attribute searchKeyword = new Attribute("searchkeyword", userSearchedKeyword);
Attribute records = new Attribute("records", String.valueOf(totalRecord));
root.addAttribute(searchKeyword);
root.addAttribute(records);
Element part_numbers = new Element("part_numbers");
for (PcFxStComPartNumberInfo info : this.gridData) {
String partNumberValue = info.getPartNum();
Element part_number = new Element("part_number");
Attribute name_attribute = new Attribute("name", partNumberValue);
part_number.addAttribute(name_attribute);
Element description = new Element("description");
String descriptionValue = info.getDescription();
description.appendChild(descriptionValue);
part_number.appendChild(description);
part_numbers.appendChild(part_number);
}
root.appendChild(part_numbers);
Document doc = new Document(root);
String result = doc.toXML();
return result;
}
}
Second Java source in: com.myapp packge. You can remove comparable if you want.
package com.myapp;
import java.util.*;
public class PcFxStComPartNumberInfo implements Comparable<PcFxStComPartNumberInfo>{
private String partNum;
private String partCode;
//private String subClassId;
//private String subClassName;
private String description;
private String status;
//private List<PcFxStComOrderablePartNumber> opnList;
public String getPartNum() {
return partNum;
}
public void setPartNum(String partNum) {
this.partNum = partNum;
}
public String getPartCode() {
return partCode;
}
public void setPartCode(String partCode) {
this.partCode = partCode;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public int compareTo(PcFxStComPartNumberInfo o) {
// TODO Auto-generated method stub
if( o==null)
return 1;
else
return this.getPartNum().compareTo(o.getPartNum());
}
}
Step3) Run your first Java Source a Java Application and see the result. Its better than DOM, SAX and StAX API.