📄 convertscriptobj.java
字号:
package com.struts.converters;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.json.simple.JSONObject;import org.w3c.dom.CharacterData;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import com.wutka.jox.JOXBeanDOM;public class ConvertScriptObj { /* * *Author:guosg */ List lis = new ArrayList(); public void add(StrutsAjaxParam sap){ lis.add(sap); } public String parse() throws IOException{ return "("+parse(new StrutsAjaxParam("root",lis))+")"; } public String parse(Object obj) throws IOException{ JSONObject jo = new JSONObject(); JOXBeanDOM beanDOM = new JOXBeanDOM(); Document doc = beanDOM.beanToDocument("root", obj); Element element = doc.getDocumentElement(); NodeList children = element.getChildNodes(); for (int i=0; i < children.getLength(); i++){ Node n = children.item(i); String objName =""; if (n instanceof Element){ NamedNodeMap attributes = n.getAttributes(); if ((attributes != null) && (attributes.getLength() > 0)) {// System.out.println("a"+"Attributes:"); for (int a=0; a < attributes.getLength(); a++) { Node attr = attributes.item(a);// System.out.println("b"+" "+attr.getNodeName()+"="+attr.getNodeValue()); if(attr.getNodeName().trim().equals("strutsAjaxName")){ objName = attr.getNodeValue(); }else if(attr.getNodeName().trim().equals("strutsAjaxValue")){ jo.put(objName, attr.getNodeValue()); } } } NodeList childValue = n.getChildNodes(); if(childValue!=null && childValue.getLength()>0){ JSONObject subJo = new JSONObject(); for (int a=0; a < childValue.getLength(); a++){ Node nValue = childValue.item(a); //String curObjName = a==0?objName:objName+"."+a; String curObjName =String.valueOf(a); if (nValue instanceof Element){ subJo.put(curObjName, parseSub((Element) nValue)); } // else if (n instanceof CharacterData){ // System.out.println(indent+" "+((CharacterData)n).getData()); // } } jo.put(objName,subJo); } }// else if (n instanceof CharacterData)// {// System.out.println(indent+" "+((CharacterData)n).getData());// } } return jo.toString(); } public JSONObject parseSub(Element el){ //String sRt = ""; JSONObject jo = new JSONObject(); NamedNodeMap attributes = el.getAttributes(); if ((attributes != null) && (attributes.getLength() > 0)){ for (int i=0; i < attributes.getLength(); i++) { Node attr = attributes.item(i); if(!attr.getNodeName().trim().equals("java-class") && attr.getNodeValue().length()>0) jo.put(attr.getNodeName(), attr.getNodeValue()); } } NodeList childValue = el.getChildNodes(); if(childValue!=null && childValue.getLength()>0){ for (int a=0; a < childValue.getLength(); a++){ Node nValue = childValue.item(a); String curObjName = a==0?nValue.getNodeName():nValue.getNodeName()+"."+a; if (nValue instanceof Element){ JSONObject tJo = parseSub((Element) nValue); if(!tJo.isEmpty()){ jo.put(curObjName, tJo); } }else if (nValue instanceof CharacterData){// System.out.println("list"+" "+((CharacterData)nValue).getData()); jo.put(nValue.getNodeName(), ((CharacterData)nValue).getData()); } } }// if(!jo.isEmpty()){// //sRt = jo.toString();// } return jo; } /** * @param args * @throws IOException * @throws ConversionException */ public static void main(String[] args) throws Exception { TestBean tb = new TestBean(); tb.setTest1("test1"); tb.setTest2("test2"); tb.setTest3("test3"); tb.getTb1().setTestBean1Name("name"); tb.getTb1().setTestBean1Value("value"); StrutsAjaxParam sap = new StrutsAjaxParam(); sap.setStrutsAjaxName("inpName"); sap.setStrutsAjaxValue("inpvalue"); List childList = new ArrayList(); childList.add("guosg"); childList.add("guosg1"); childList.add("guosg2"); List lis = new ArrayList(); lis.add(new StrutsAjaxParam("tb",tb)); lis.add(new StrutsAjaxParam("childList",childList)); lis.add(sap); StrutsAjaxParam sap1 = new StrutsAjaxParam("root",lis); //Test t = new Test(); // JOXBeanDOM beanDOM = new JOXBeanDOM();// Document doc = beanDOM.beanToDocument("root", sap1);// Element element = doc.getDocumentElement();// dumpElement(element, "");// BeanConverter bc = new BeanConverter();// String s =bc.convertOutbound(sap1, "test", null);// System.out.print(s); ConvertScriptObj tx = new ConvertScriptObj(); //tx.parse(sap1); System.out.print(tx.parse(sap1)); } public static void dumpElement(Element element, String indent) { System.out.println(indent+"Element: "+element.getNodeName()); NamedNodeMap attributes = element.getAttributes(); if ((attributes != null) && (attributes.getLength() > 0)) { System.out.println(indent+"Attributes:"); for (int i=0; i < attributes.getLength(); i++) { Node attr = attributes.item(i); System.out.println(indent+" "+attr.getNodeName()+"="+attr.getNodeValue()); } } NodeList children = element.getChildNodes(); for (int i=0; i < children.getLength(); i++) { Node n = children.item(i); if (n instanceof Element) { dumpElement((Element) n, indent+" "); } else if (n instanceof CharacterData) { System.out.println(indent+" "+((CharacterData)n).getData()); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -