XMLSerializer把xml转json
InputStreamReaderreader=null;try{try{reader=newInputStreamReader(newFileInputStream("...
InputStreamReader reader = null;
try {
try {
reader = new InputStreamReader(new FileInputStream("E://1.xml"),"UTF-8");
} catch (UnsupportedEncodingException e) {
logger.error("读取文件路径异常"+e);
return null;
}
} catch (FileNotFoundException e) {
logger.error("文件路径未找到"+e);
return null;
}
StringBuffer sb = new StringBuffer();
int ch = 0;
// 读取xml文件内容
try {
while ((ch = reader.read()) != -1) {
sb.append((char) ch);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
xml = sb.toString();
// 打包成json格式的数据,并进行相应的转换
int low = 97;
int upp = 65;
for (int j = 0; j < 25; j++) {
xml = xml.replaceAll("_" + (char) low, "_" + (char) upp);
upp++;
low++;
}
xml:
<drinkOrder></drinkOrder><drinksOnly></drinksOnly><taxFlag1></taxFlag1><taxFlag2>Y</taxFlag2>
json:
"drinkOrder":[],"drinksOnly":[],"taxFlag1":[],"taxFlag2":"Y"
问题:当节点之间为空串时,就会把它解析成[],这个问题有什么办法能解决,没财富了,求好心人解答 展开
try {
try {
reader = new InputStreamReader(new FileInputStream("E://1.xml"),"UTF-8");
} catch (UnsupportedEncodingException e) {
logger.error("读取文件路径异常"+e);
return null;
}
} catch (FileNotFoundException e) {
logger.error("文件路径未找到"+e);
return null;
}
StringBuffer sb = new StringBuffer();
int ch = 0;
// 读取xml文件内容
try {
while ((ch = reader.read()) != -1) {
sb.append((char) ch);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
xml = sb.toString();
// 打包成json格式的数据,并进行相应的转换
int low = 97;
int upp = 65;
for (int j = 0; j < 25; j++) {
xml = xml.replaceAll("_" + (char) low, "_" + (char) upp);
upp++;
low++;
}
xml:
<drinkOrder></drinkOrder><drinksOnly></drinksOnly><taxFlag1></taxFlag1><taxFlag2>Y</taxFlag2>
json:
"drinkOrder":[],"drinksOnly":[],"taxFlag1":[],"taxFlag2":"Y"
问题:当节点之间为空串时,就会把它解析成[],这个问题有什么办法能解决,没财富了,求好心人解答 展开
1个回答
展开全部
1. JavaScript代码如下:
复制代码代码如下:
// Changes XML to JSON
function xmlToJson(xml) {
// Create the return object
var obj = {};
if (xml.nodeType == 1) { // element
// do attributes
if (xml.attributes.length > 0) {
obj["@attributes"] = {};
for (var j = 0; j < xml.attributes.length; j++) {
var attribute = xml.attributes.item(j);
obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
}
}
} else if (xml.nodeType == 3) { // text
obj = xml.nodeValue;
}
// do children
if (xml.hasChildNodes()) {
for(var i = 0; i < xml.childNodes.length; i++) {
var item = xml.childNodes.item(i);
var nodeName = item.nodeName;
if (typeof(obj[nodeName]) == "undefined") {
obj[nodeName] = xmlToJson(item);
} else {
if (typeof(obj[nodeName].length) == "undefined") {
var old = obj[nodeName];
obj[nodeName] = [];
obj[nodeName].push(old);
}
obj[nodeName].push(xmlToJson(item));
}
}
}
return obj;
};
2. XML代码:
复制代码代码如下:
<ALEXA VER="0.9" URL="davidwalsh.name/" HOME="0" AID="=">
<SD TITLE="A" FLAGS="" HOST="davidwalsh.name">
<TITLE TEXT="David Walsh Blog :: PHP, MySQL, CSS, Javascript, MooTools, and Everything Else"/>
<LINKSIN NUM="1102"/>
<SPEED TEXT="1421" PCT="51"/>
</SD>
<SD>
<POPULARITY URL="davidwalsh.name/" TEXT="7131"/>
<REACH RANK="5952"/>
<RANK DELTA="-1648"/>
</SD>
</ALEXA>
3. JSON结果:
复制代码代码如下:
{
"@attributes": {
AID: "=",
HOME: 0,
URL: "davidwalsh.name/",
VER: "0.9",
},
SD = [
{
"@attributes": {
FLAGS: "",
HOST: "davidwalsh.name",
TITLE: A
},
LINKSIN: {
"@attributes": {
NUM: 1102
}
},
SPEED: {
"@attributes": {
PCT: 51,
TEXT: 1421
}
},
TITLE: {
"@attributes": {
TEXT: "David Walsh Blog :: PHP, MySQL, CSS, Javascript, MooTools, and Everything Else",
}
},
},
{
POPULARITY: {
"@attributes": {
TEXT: 7131,
URL: "davidwalsh.name/"
}
},
RANK: {
"@attributes": {
DELTA: "-1648"
}
},
REACH: {
"@attributes": {
RANK = 5952
}
}
}
]
}
复制代码代码如下:
// Changes XML to JSON
function xmlToJson(xml) {
// Create the return object
var obj = {};
if (xml.nodeType == 1) { // element
// do attributes
if (xml.attributes.length > 0) {
obj["@attributes"] = {};
for (var j = 0; j < xml.attributes.length; j++) {
var attribute = xml.attributes.item(j);
obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
}
}
} else if (xml.nodeType == 3) { // text
obj = xml.nodeValue;
}
// do children
if (xml.hasChildNodes()) {
for(var i = 0; i < xml.childNodes.length; i++) {
var item = xml.childNodes.item(i);
var nodeName = item.nodeName;
if (typeof(obj[nodeName]) == "undefined") {
obj[nodeName] = xmlToJson(item);
} else {
if (typeof(obj[nodeName].length) == "undefined") {
var old = obj[nodeName];
obj[nodeName] = [];
obj[nodeName].push(old);
}
obj[nodeName].push(xmlToJson(item));
}
}
}
return obj;
};
2. XML代码:
复制代码代码如下:
<ALEXA VER="0.9" URL="davidwalsh.name/" HOME="0" AID="=">
<SD TITLE="A" FLAGS="" HOST="davidwalsh.name">
<TITLE TEXT="David Walsh Blog :: PHP, MySQL, CSS, Javascript, MooTools, and Everything Else"/>
<LINKSIN NUM="1102"/>
<SPEED TEXT="1421" PCT="51"/>
</SD>
<SD>
<POPULARITY URL="davidwalsh.name/" TEXT="7131"/>
<REACH RANK="5952"/>
<RANK DELTA="-1648"/>
</SD>
</ALEXA>
3. JSON结果:
复制代码代码如下:
{
"@attributes": {
AID: "=",
HOME: 0,
URL: "davidwalsh.name/",
VER: "0.9",
},
SD = [
{
"@attributes": {
FLAGS: "",
HOST: "davidwalsh.name",
TITLE: A
},
LINKSIN: {
"@attributes": {
NUM: 1102
}
},
SPEED: {
"@attributes": {
PCT: 51,
TEXT: 1421
}
},
TITLE: {
"@attributes": {
TEXT: "David Walsh Blog :: PHP, MySQL, CSS, Javascript, MooTools, and Everything Else",
}
},
},
{
POPULARITY: {
"@attributes": {
TEXT: 7131,
URL: "davidwalsh.name/"
}
},
RANK: {
"@attributes": {
DELTA: "-1648"
}
},
REACH: {
"@attributes": {
RANK = 5952
}
}
}
]
}
追问
文件我全部生成好了,就是在转换的时候格式出错了,要怎么解决
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询