我正在嘗試編寫XML文檔並將其POST,但在使用XmlSerializer(我在Android Studio中工作)時無法獲得正確的文檔格式。JAVA:使用Xmlserializer編寫xml - 上下文問題
我希望寫的XML:
<?xml version="1.0" encoding="UTF-8"?>
<ser:serviceParameters xmlns:ser="http://www.endinfosys.com/Voyager/serviceParameters">
<ser:parameters>
<ser:parameter key="action">
<ser:value>add</ser:value>
</ser:parameter>
</ser:parameters>
<ser:patronIdentifier lastName="LastName" patronHomeUbId="[email protected]" patronId="12345">
<ser:authFactor type="B">150</ser:authFactor>
</ser:patronIdentifier>
<ser:definedParameters xsi:type="pat:bookbagRecordsType" xlns:pat="http://www.endinfosys.com/Voyager/patronPreferences" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<pat:record>
<pat:bibId>54414</pat:bibId>
<pat:databaseCode>LOCAL</pat:databaseCode>
</pat:record>
</ser:definedParameters>
</ser:serviceParameters>
這是我必須生產代碼XML:
private static String writeXml(String surname, String barcode, String thisbibid){
XmlSerializer serializer = Xml.newSerializer();
StringWriter writer = new StringWriter();
try {
serializer.setOutput(writer);
serializer.startDocument("UTF-8", true);
serializer.startTag("ser", "serviceParameters");
serializer.attribute("http://www.endinfosys.com/Voyager/serviceParameters", "xmlns", "ser");
serializer.startTag("ser", "parameters");
serializer.startTag("ser", "parameter");
serializer.attribute("ser", "action", "add");
serializer.endTag("ser", "parameter");
serializer.endTag("ser", "parameters");
serializer.startTag("ser", "patronIdentifier");
serializer.attribute("", "lastName", surname);
serializer.attribute("", "patronHomeUbId", "local");
serializer.attribute("", "patronId", "12345");
serializer.startTag("ser", "authFactor");
serializer.attribute("", "type", "B");
serializer.text("150");
serializer.endTag("ser", "authFactor");
serializer.endTag("ser", "patronIdentifier");
serializer.startTag("ser", "definedParameters");
serializer.attribute("http://www.endinfosys.com/Voyager/patronPreferences", "xmlns", "pat");
serializer.attribute("pat", "bookbagRecordsType", "xsi:type");
serializer.startTag("pat", "record");
serializer.startTag("pat", "bibId");
serializer.text(thisbibid);
serializer.endTag("pat", "bibId");
serializer.startTag("pat", "databaseCode");
serializer.text("LOCAL");
serializer.endTag("pat", "databaseCode");
serializer.endTag("pat", "record");
serializer.endTag("ser", "definedParameters");
serializer.endTag("ser", "serviceParameters");
serializer.endDocument();
return writer.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
但是字符串輸出爲:
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><n0:serviceParameters n1:xmlns="ser" xmlns:n0="ser" xmlns:n1="http://www.endinfosys.com/Voyager/serviceParameters"><n0:parameters><n0:parameter n0:action="add" /></n0:parameters><n0:patronIdentifier lastName="LastName" patronHomeUbId="local" patronId="12345"><n0:authFactor type="B">150</n0:authFactor></n0:patronIdentifier><n0:definedParameters n2:xmlns="pat" n3:bookbagRecordsType="xsi:type" xmlns:n2="http://www.endinfosys.com/Voyager/patronPreferences" xmlns:n3="pat"><n3:record><n3:bibId>54414</n3:bibId><n3:databaseCode>LOCAL</n3:databaseCode></n3:record></n0:definedParameters></n0:serviceParameters>
我已嘗試改變.attribute元素的順序,但無法正確獲取前綴。任何幫助,將不勝感激
import org.xmlpull.v1.XmlSerializer;我想這就是你要求的。代碼不會拋出任何錯誤,它是錯誤的XML格式,帶有n0前綴。 – JamieDenman