Home > Back-end >  Using the XMLSerializer XML to json and found a problem, need background processing!!!!!! Consult a
Using the XMLSerializer XML to json and found a problem, need background processing!!!!!! Consult a

Time:12-05


















Through the xmlSerializer. Read (XML). The toString ();
Conversion results:

{
"@ nameCN" : "electric logging,"
"Step" : [
{
"signer" : {
"@ staffID" : "800706",
"@ nameCN" : ", "
"# cdata section -" :
}
},
{
"signer" : {
"@ staffID" : "090477",
"@ nameCN" : ", "
"# cdata section -" :
}

}
]
},
{
"@ nameCN" : "energy conservation",
"Step" : [
{
"Signer" : [
{
"@ staffID" : "800608",
"@ nameCN" : ", "
"# cdata section -" :
},
{
"@ staffID" : "800808",
"@ nameCN" : ", "
"# cdata section -" :
}
]
},
{
"Signer" : [
{
"@ staffID" : "800602",
"@ nameCN" : ", "
"# cdata section -" :
},
{
"@ staffID" : "800803",
"@ nameCN" : ", "
"# cdata section -" :
}
]
}
]
}

CodePudding user response:

Result of the above marked in red is different, have more than one signer under step nodes, the output signer is an array,

Only one output signer signer node is not an array, how to when there is only one signer node output for an array

CodePudding user response:

Gaiyuan code? Couldn't think of any other measures epsilon=(' &western ` *))) ~

CodePudding user response:

refer to the second floor u011594442 response:
gaiyuan code? Can't think of any other measures epsilon=(' &western ` *))) ~


Also somebody in this kind of problem before, only one included methods:
There is a program, but is not good, also is the XML deserialization, first to write classes to deserialize deserialized, which is then serialized into json,
The page with a plug-in test whether node as an array, not into the array;
But I this is others interface background request I

CodePudding user response:

Bloggers, is there any code conversion, I XML to json cdata section directly display content without cdata logo inside,

CodePudding user response:

Speculation is that XML is not formatted, tested, if is simulated to submit standard XML format, with flow read in all right, you won't get switched array error object, if others submitted is a line of string will convert exceptions, take your XML string under this method is good
Public static String formatXML XML (String) {
String requestXML=null;
Try {
//take the parser
SAXReader reader=new SAXReader ();
The Document Document=reader. Read (new StringReader (XML));
if (null !={
the document)StringWriter StringWriter=new StringWriter ();
//formatting, space for each level of the former
OutputFormat format=new OutputFormat (" ", true);
//the XML declaration and the content is to add a blank line
Format. SetNewLineAfterDeclaration (false);
//set the XML declaration head
Format. SetSuppressDeclaration (false);
//whether the branch
Format. SetNewlines (true);
XMLWriter writer=new XMLWriter (stringWriter, format);
Writer. Write (document);
Writer. Flush ();
Writer. The close ();
RequestXML=stringWriter. GetBuffer (). The toString ();
}
Return requestXML;
} the catch (Exception e) {
e.printStackTrace();
return null;
}
}
  • Related