web services 中不规则XML格式传送转成标准的XML
最近公司要做应用集成,涉及到很多的接口集成,而传入过来的XML并不是标准的XML所以我想把非标准的XML转成标准的,以便于用标准的XML可以直接当做表插入数据库。
这是我所写的一个测试类:
package com.dcms.util.webServices;
public class ECToXML {
public static String[] split(String str , char ch){
if(str==null){
return null;
}
if(str.charAt(0)==ch){
str = str.substring(1);
}
if(str.charAt(str.length()-1)==ch){
str = str.substring(0,str.length()-1);
}
return str.split(ch+"");
}
/**
* @param args
*/
public static void main(String[] args) {
String xml = "<STD_IN Origin="TIPTOP"><Service Name="SetData"><ObjectID>Item</ObjectID><ServiceId>TIPTOP</ServiceId><Operate>ADJUST</Operate><Seperator>^*^</Seperator><User>cdlc</User><Factory>Unknown</Factory><IgnoreError>N</IgnoreError><Data Format="Join"><DataSet Field="productId^*^productName^*^scale^*^assort2^*^type1^*^packageSize^*^resellPrice^*^assortment^*^barcode^*^invCount^*^invUnit^*^mInvClass^*^preDays^*^status^*^updateDate^*^type^*^stInPrice^*^itemSeq^*^packageNo^*^quotiety^*^length^*^width^*^height^*^volume^*^netWeight^*^grossWeight^*^sceneId^*^sceneName^*^brandId^*^brandName^*^invUnitName^*^itemSeqName^*^unit^*^unitName^*^poUnit^*^poUnitName"><Row Data="902000001^*^J2551JA长几^*^1418*875*380^*^001^*^B^*^^*^222^*^333^*^^*^53^*^PCS^*^S091^*^0^*^Y^*^2010-05-04 00:00:00^*^0^*^1961^*^L02^*^1^*^1^*^1490^*^940^*^60^*^.084^*^46^*^48^*^101^*^客厅^*^L02^*^依洛歌^*^PCS^*^依洛歌^*^PCS^*^PCS^*^PCS^*^PCS"/><Row Data="902000001^*^J2551JA长几^*^1418*875*380^*^001^*^B^*^^*^0^*^001^*^^*^53^*^PCS^*^S091^*^0^*^Y^*^2010-05-04 00:00:00^*^0^*^1961^*^L02^*^2^*^1^*^1360^*^960^*^130^*^.17^*^44^*^46^*^101^*^客厅^*^L02^*^依洛歌^*^PCS^*^依洛歌^*^PCS^*^PCS^*^PCS^*^PCS"/></DataSet></Data></Service></STD_IN>";
LBToECXML ec = new LBToECXML();
String tt = ec.toXML(xml);
//String temp = "abbcbbdbbebb";
//int a = temp.indexOf("bb");
//String temp1 = temp.substring(0, temp.indexOf("bb"));
//System.out.println(temp1);
//System.out.println(a);
}
}
下面是调用的方法:
package com.dcms.util.webServices;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class LBToECXML {
public Document parse(String url) throws DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read(url);
return document;
}
public String bar(Document document,String bean ,String[] dateList,String[] valueList) throws DocumentException {
Element root = document.getRootElement();
String xmlDoc ="<?xml version="1.0" encoding="UTF-8"?>";
xmlDoc = xmlDoc + "<" + bean + ">";
String dateSet ="";
// iterate through child elements of root
List list = new ArrayList();
for (Iterator i = root.elementIterator(); i.hasNext();) {
Element element = (Element) i.next();
if(bean.equals(element.attributeValue("name"))){
for (Iterator i1 = element.elementIterator(); i1.hasNext();) {
Element e1 = (Element) i1.next();
if("DetailObjId".equals(e1.getName())){
dateSet = e1.getText();
}
if("MasterField".equals(e1.getName())){
for (int j = 0; j < dateList.length; j++) {
if(dateList[j].equals(e1.attributeValue("RefErp"))){
xmlDoc = xmlDoc + "<" + e1.getText() + ">";
if(valueList!=null&&valueList.length>0&&valueList.length>j){
xmlDoc = xmlDoc + valueList[j] + "</" +e1.getText() + ">";
}else{
xmlDoc = xmlDoc +"</" +e1.getText() + ">";
}
}
}
}else if("DetailField".equals(e1.getName())){
list.add(e1.getText());
}
}
}
}
xmlDoc = xmlDoc + "<" + dateSet + ">";
for (int j = 0; j < dateList.length; j++) {
xmlDoc = xmlDoc + "<" + list.get(j) + ">";
if(valueList!=null&&valueList.length>0&&valueList.length>j){
xmlDoc = xmlDoc + valueList[j] + "</" +list.get(j) + ">";
}else{
xmlDoc = xmlDoc +"</" +list.get(j) + ">";
}
}
xmlDoc = xmlDoc + "</" + dateSet + ">";
xmlDoc = xmlDoc + "</" + bean + ">";
//for (Iterator i = root.elementIterator(); i.hasNext();) {
//Element element = (Element) i.next();
//System.out.println(element.getName() + " "
//+ element.attributeValue("name"));
//System.out.println(bean);
//System.out.println(element.attributeValue("name").equals(bean));
//if(element.attributeValue("name").equals(bean)){
//for (Iterator i1 = element.elementIterator(); i1.hasNext();) {
//Element e1 = (Element) i1.next();
//for (int j = 0; j < dateList.length; j++) {
//if(dateList[j].equals(e1.attributeValue("RefErp"))){
// System.out.println(e1.getText());
//}
//}
//}
//}
////System.out.println("________________");
////System.out.println(element.getName() + " "
////+ element.attributeValue("name"));
//for (Iterator i1 = element.elementIterator(); i1.hasNext();) {
////Element e1 = (Element) i1.next();
////System.out.println("+++++++++"+ e1.attributeValue("RefErp") );
////System.out.println(e1.getName() + " = " + e1.getText());
//}
//}
return xmlDoc;
}
public String barList(Document document,String bean ,String[] dateList,String[][] valuelist,String[] valueListTField) throws DocumentException {
Element root = document.getRootElement();
String xmlDoc ="<?xml version="1.0" encoding="UTF-8"?>";
xmlDoc = xmlDoc + "<" + bean + ">";
String dateSet ="";
// iterate through child elements of root
List list = new ArrayList();
for (Iterator i = root.elementIterator(); i.hasNext();) {
Element element = (Element) i.next();
if(bean.equals(element.attributeValue("name"))){
for (Iterator i1 = element.elementIterator(); i1.hasNext();) {
Element e1 = (Element) i1.next();
if("DetailObjId".equals(e1.getName())){
dateSet = e1.getText();
}
if("MasterField".equals(e1.getName())){
for (int j = 0; j < dateList.length; j++) {
if(dateList[j].equals(e1.attributeValue("RefErp"))){
xmlDoc = xmlDoc + "<" + e1.getText() + ">";
if(valueListTField!=null&&valueListTField.length>0&&valueListTField.length>j){
xmlDoc = xmlDoc + valueListTField[j] + "</" +e1.getText() + ">";
}else{
xmlDoc = xmlDoc +"</" +e1.getText() + ">";
}
}
}
}
}
}
}
for (Iterator i = root.elementIterator(); i.hasNext();) {
Element element = (Element) i.next();
if(bean.equals(element.attributeValue("name"))){
for (int k = 0; k < valuelist.length; k++) {
xmlDoc = xmlDoc + "<" + dateSet + ">";
for (Iterator i1 = element.elementIterator(); i1.hasNext();) {
Element e1 = (Element) i1.next();
if("DetailObjId".equals(e1.getName())){
dateSet = e1.getText();
}
if("DetailField".equals(e1.getName())){
for (int j = 0; j < valuelist[k].length; j++) {
if(dateList[j].equals(e1.attributeValue("RefErp"))){
xmlDoc = xmlDoc + "<" + e1.getText() + ">";
if(valueListTField!=null&&valueListTField.length>0&&valueListTField.length>j){
xmlDoc = xmlDoc + valuelist[k][j] + "</" +e1.getText() + ">";
}else{
xmlDoc = xmlDoc +"</" +e1.getText() + ">";
}
}
}
}
}
xmlDoc = xmlDoc + "</" + dateSet + ">";
}
}
}
//xmlDoc = xmlDoc + "<" + dateSet + ">";
//for (int j = 0; j < dateList.length; j++) {
// xmlDoc = xmlDoc + "<" + list.get(j) + ">";
// if(valueList!=null&&valueList.length>0&&valueList.length>j){
// xmlDoc = xmlDoc + valueList[j] + "</" +list.get(j) + ">";
// }else{
// xmlDoc = xmlDoc +"</" +list.get(j) + ">";
// }
//
//}
//xmlDoc = xmlDoc + "</" + dateSet + ">";
xmlDoc = xmlDoc + "</" + bean + ">";
return xmlDoc;
}
public String toXML(String xml){
if(xml.indexOf("<Row Data=")==(xml.substring(0, xml.lastIndexOf("<Row Data="))).length()){
String data = xml;
String split = xml.substring(xml.indexOf("<Seperator>")+11, xml.indexOf("</Seperator>"));
String boBean = xml.substring(xml.indexOf("<ObjectID>")+10, xml.indexOf("</ObjectID>"));
String field = xml.substring(xml.indexOf("<DataSet Field=")+16, xml.indexOf("><Row Data=")-1);
String fieldValue = xml.substring(xml.indexOf("<Row Data=")+11, xml.indexOf("/>")-1);
//LB传入过来的字段名称
String[] fieldList = field.split("\\^\\*\\^");
String tempValue = xml.substring(xml.indexOf("<Row Data="), xml.indexOf("</DataSet>"));
String[] valueListT = null;
String[] valueList = tempValue.split("<Row Data=");
for (int i = 0; i < valueList.length; i++) {
String tt = valueList[i];
if(tt!=null&&tt.length()>0){
System.out.println(tt);
tt = tt.substring(1, tt.length()-3);
System.out.println(tt);
//LB传入过来的所有字段对应的值。
valueListT= tt.split("\\^\\*\\^");
}
}
try {
LBToECXML foo = new LBToECXML();
Document doc = foo.parse("src/EcToXML.xml");
String xmlReturn = foo.bar(doc,boBean,fieldList,valueListT);
System.out.println(xmlReturn);
}catch (DocumentException e) {
e.printStackTrace();
}
return"";
}else{
String data = xml;
int count = 0;
String temp = xml;
String vaTemp = xml;
String split = xml.substring(xml.indexOf("<Seperator>")+11, xml.indexOf("</Seperator>"));
String boBean = xml.substring(xml.indexOf("<ObjectID>")+10, xml.indexOf("</ObjectID>"));
String field = xml.substring(xml.indexOf("<DataSet Field=")+16, xml.indexOf("><Row Data=")-1);
String fieldValueField = xml.substring(xml.indexOf("<Row Data=")+11, xml.indexOf("/>")-1);
String[] fieldList = field.split("\\^\\*\\^");
String[] valueListTField = fieldValueField.split("\\^\\*\\^");
while(temp.indexOf("<Row Data=")>0){
count ++ ;
int tt = temp.indexOf("<Row Data=");
temp = temp.substring(temp.indexOf("<Row Data=")+16, temp.length()-1);
}
String fieldlist [][] = new String [count][];
String valuelistT [][] = new String [count][];
for(int i = 0 ;i<count;i++){
String fieldValue = vaTemp.substring(vaTemp.indexOf("<Row Data=")+11, vaTemp.indexOf("/>")-1);
String[] valueListT = fieldValue.split("\\^\\*\\^");
valuelistT [i] = new String [valueListT.length];
for (int j = 0; j < valueListT.length; j++) {
valuelistT[i][j] = valueListT[j];
}
vaTemp = vaTemp.substring(vaTemp.indexOf("<Row Data=")+11, vaTemp.length()-1);
vaTemp = vaTemp.substring(vaTemp.indexOf("/>")+1, vaTemp.length()-1);
}
try {
LBToECXML foo = new LBToECXML();
Document doc = foo.parse("src/EcToXML.xml");
String xmlReturn = foo.barList(doc,boBean,fieldList,valuelistT,valueListTField);
System.out.println(xmlReturn);
}catch (DocumentException e) {
e.printStackTrace();
}
return "";
}
}
}
输出结果是:
<?xml version="1.0" encoding="UTF-8"?>
<Item>
<ItemNo>902000001</ItemNo>
<DeliveredQty>J2551JA长几</DeliveredQty>
<Closed>1418*875*380</Closed>
<ItemNo>001</ItemNo>
<DeliveredQty>B</DeliveredQty>
<Closed></Closed>
<ItemOrder>
<ItemNo>222</ItemNo>
<DeliveredQty>333</DeliveredQty>
<Closed></Closed>
<DeliveredQty>53</DeliveredQty>
<Closed>PCS</Closed>
<ItemNo>S091</ItemNo>
<DeliveredQty>0</DeliveredQty>
<Closed>Y</Closed>
<Closed>2010-05-04 00:00:00</Closed>
<ItemNo>0</ItemNo>
<DeliveredQty>1961</DeliveredQty>
<Closed>L02</Closed>
<ItemNo>1</ItemNo>
<DeliveredQty>1</DeliveredQty>
<Closed>1490</Closed>
<ItemNo>940</ItemNo>
<DeliveredQty>60</DeliveredQty>
<Closed>.084</Closed>
<DeliveredQty>46</DeliveredQty>
<Closed>48</Closed>
<ItemNo>101</ItemNo>
<DeliveredQty>客厅</DeliveredQty>
<Closed>L02</Closed>
<Closed>依洛歌</Closed>
<ItemNo>PCS</ItemNo>
<DeliveredQty>依洛歌</DeliveredQty>
<Closed>PCS</Closed>
<ItemNo>PCS</ItemNo>
<DeliveredQty>PCS</DeliveredQty>
<DeliveredQty>PCS</DeliveredQty>
</ItemOrder>
<ItemOrder>
<ItemNo>0</ItemNo>
<DeliveredQty>001</DeliveredQty>
<Closed></Closed>
<DeliveredQty>53</DeliveredQty>
<Closed>PCS</Closed>
<ItemNo>S091</ItemNo>
<DeliveredQty>0</DeliveredQty>
<Closed>Y</Closed>
<Closed>2010-05-04 00:00:00</Closed>
<ItemNo>0</ItemNo>
<DeliveredQty>1961</DeliveredQty>
<Closed>L02</Closed>
<ItemNo>2</ItemNo>
<DeliveredQty>1</DeliveredQty>
<Closed>1360</Closed>
<ItemNo>960</ItemNo>
<DeliveredQty>130</DeliveredQty>
<Closed>.17</Closed>
<DeliveredQty>44</DeliveredQty>
<Closed>46</Closed>
<ItemNo>101</ItemNo>
<DeliveredQty>客厅</DeliveredQty>
<Closed>L02</Closed>
<Closed>依洛歌</Closed>
<ItemNo>PCS</ItemNo>
<DeliveredQty>依洛歌</DeliveredQty>
<Closed>PCS</Closed>
<ItemNo>PCS</ItemNo>
<DeliveredQty>PCS</DeliveredQty>
<DeliveredQty>PCS</DeliveredQty>
</ItemOrder>
</Item>