gpt4 book ai didi

java - 编码后 XML 未完全假脱机到文件中

转载 作者:行者123 更新时间:2023-12-01 04:14:41 25 4
gpt4 key购买 nike

我正在尝试编码一个对象并替换一些无效的字符。在此过程中,不会生成完整的 xml。我在所有生成的文件中只能看到 1024 个字符。

package com.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.Scanner;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamResult;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.apache.log4j.Logger;
import org.xml.sax.SAXException;
import com.sun.xml.bind.marshaller.NamespacePrefixMapper;

public class MessageParserComponent {
private static final Logger LOGGER = Logger.getLogger(MessageParserComponent.class);

public File marshalIXml(final Object obj, final String xsdSchema,
final String xmlFileName, final JAXBContext ctx) {
File xml = new File(xmlFileName);

try {
xml.createNewFile();
Marshaller marshaller = null;
marshaller = ctx.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
"http://www.db.com/tf " + xsdSchema);
marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper",
new NamespacePrefixMapper() {
@Override
public String getPreferredPrefix(String arg0, String arg1,
boolean arg2) {
return "tf";
}
});

marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
"http://www.db.com/tf " + xsdSchema);
marshaller.setSchema(getSchema(xsdSchema));
marshaller.marshal(obj, new StreamResult(xml));
xml = replaceInvalidChar('\u0007', '\n', xml);
xml = replaceInvalidString("ns2", "xsi", xml);
} catch (IOException e) {
LOGGER.error(e);
} catch (JAXBException e) {
LOGGER.error(e);
} catch (SAXException e) {
LOGGER.error(e);
}
return xml;
}

private Schema getSchema(String xsdSchema) throws SAXException {
SchemaFactory fact = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = fact.newSchema(this.getClass().getClassLoader()
.getResource(xsdSchema));
return schema;
}

private static File replaceInvalidString(String Target, String Dest,
File Source) throws IOException {
String xml_string;
xml_string = new Scanner(Source).useDelimiter("\\Z").next();
xml_string = xml_string.replace(Target, Dest);
FileOutputStream fi = new FileOutputStream(Source);
fi.write(xml_string.getBytes());
return Source;
}

public static File replaceInvalidChar(char Target, char Dest, File Source)
throws IOException {
String xml_string;
xml_string = new Scanner(Source).useDelimiter("\\Z").next();
xml_string = xml_string.replace(Target, Dest);
FileOutputStream fi = new FileOutputStream(Source);
fi.write(xml_string.getBytes());
return Source;
}
}

Is there a limit for string replacement?
Am I creating the file in a wrong way?

注意:

I'm storing file in UNIX log folder
I have java 6, JAXB 2.2

我们非常感谢任何形式的帮助。

最佳答案

只需检查您是否正确地使用 jaxb 注释对对象进行了注释。为什么要设置 Marshaller 属性?您想使用外部模式来编码您的对象吗?相反,为什么不让 jaxb 来处理所有这些事情(如果您的对象在同一工作区中可用)。

这个示例程序可能对您有帮助。 http://www.mkyong.com/java/jaxb-hello-world-example/

也检查一下..

JAXB Marshaller : StringWriter output has a truncated tag value截断标签值

关于java - 编码后 XML 未完全假脱机到文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19549111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com