我有xsd架构..如何在java中以编程方式使用此架构生成xml?它应该是动态的,意味着我可以给出任何架构。 有没有可用的图书馆。? 我也看到了另一个帖子,但不幸的是它不适合我..请给出游览的想法.... ??? 我没有找到任何相同的方法。
答案 0 :(得分:0)
XPath不是用于生成XML的工具。我担心你试图用错误的工具来实现你的目标。所以,我认为你的问题的答案是:你做不到。
答案 1 :(得分:0)
看一下这个链接:
JAVA: Build XML document using XPath expressions
在XPath标题下浏览此链接:
http://www.vogella.de/articles/JavaXML/article.html
我认为这应该可以帮助你...... :)
答案 2 :(得分:0)
以下代码根据您的需要执行验证, 并且还会告诉您解析期间发生错误的行号 您将需要以下jar来运行此代码
只需要更改代码就是validateSchema()方法,其中请求响应args需要替换为xSD的字符串表示形式和XSD的String表示形式。
resolver.jar,xml-apis.jar,serializer.jar,xercesImpl.jar
import org.apache.xerces.parsers.SAXParser;
import org.apache.xerces.xni.Augmentations;
import org.apache.xerces.xni.NamespaceContext;
import org.apache.xerces.xni.XMLLocator;
import org.apache.xerces.xni.XNIException;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Performs validation on XML based on the XSD. Overrides the
* {@link SAXParser}.
*
*
*/
public class ValidateSchema extends SAXParser {
/**
* Container for current line and coloum number being parsed in the XML.
*/
private XMLLocator locator;
/**
* Default public constructor.
*/
public ValidateSchema() {
super();
}
/**
* Used for obtaining the {@link XMLLocator} locator object.
*/
@Override
public void startDocument(
XMLLocator locator, String encoding, NamespaceContext namespaceContext, Augmentations augs)
throws XNIException {
this.locator = locator;
super.startDocument(locator, encoding, namespaceContext, augs);
}
/**
* Validates the XML against the provided XSD.
*
* @param req HttpServletRequest object.
* @param resp HttpServletResponse object.
* @throws IOException
*/
public void validateSchema(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
String content = req.getParameter("data");
String selectbox = req.getParameter("selectbox");
content = content.trim();
// Convert the XML string to byte stream.
InputStream is = new ByteArrayInputStream(content.getBytes());
try {
this.setFeature(Constants.VALIDATION_PROP, true);
this.setFeature(Constants.SCHEMA_PROP, true);
this.setFeature(Constants.DYNAMIC_PROP, true);
this.setFeature(Constants.SCHEMA_CHECKING_PROP, true);
if("1".equalsIgnoreCase(selectbox)) {
this.setProperty(Constants.SCHEMA_LOC,"oem.xsd" );
} else if("2".equalsIgnoreCase(selectbox)) {
this.setProperty(Constants.SCHEMA_LOC,"carrier.xsd" );
}
Validator handler = new Validator();
this.setErrorHandler(handler);
InputSource isp = new InputSource();
isp.setByteStream(is);
isp.setEncoding("UTF-8");
this.parse(isp);
if (handler.validationError == true) {
StringBuffer errorMessage = new StringBuffer(512);
errorMessage
.append("<div style='background: #ffebe6;border: 0px solid #ffe0d7;color:#c10000;height:60px;padding: 5px;'>")
.append(Constants.INVALID_XML_DOCUMENT)
.append(" LineNo: ")
.append(handler.saxParseException.getLineNumber())
.append(" ColumnNo: ")
.append(handler.saxParseException.getColumnNumber())
.append("<br/>")
.append(handler.validationError)
.append(handler.saxParseException.getMessage())
.append("</div>");
System.out.println( errorMessage );
} else {
StringBuffer validMsg = new StringBuffer(512);
validMsg.append("<div style='background: #ebeff9;border: 0px solid #6b90da;height:60px;padding: 5px;'>")
.append(Constants.VALID_XML_DOCUMENT).append("</div>");
System.out.println( validMsg );
}
} catch (SAXException e) {
StringBuffer errorMessage = new StringBuffer(512);
errorMessage
.append("<div style='background: #ffebe6;border: 0px solid #ffe0d7;color:#c10000;height:60px;padding: 5px;'>")
.append(Constants.INVALID_XML_DOCUMENT)
.append(" LineNo: ")
.append(this.locator.getLineNumber())
.append(" ColumnNo: ")
.append(this.locator.getColumnNumber())
.append(" <br/>")
.append(e.getMessage())
.append("</div>");
System.out.println( errorMessage );
} catch (Exception e) {
StringBuffer errorMessage = new StringBuffer(512);
errorMessage
.append("<div style='background: #ffebe6;border: 1px solid #ffe0d7;color:#c10000;height:60px;padding: 5px;'>")
.append(Constants.INVALID_XML_DOCUMENT)
.append(" <br/>")
.append(e.getMessage())
.append("</div>");
System.out.println( errorMessage );
}
}
/**
* Writes back the response to client.
*
* @param msg Response message.
* @param resp HttpServletResponse object.
* @throws IOException
*/
private void responseWrite(
String msg, HttpServletResponse resp) throws IOException {
resp.setContentType("text/html");
resp.getWriter().write(msg);
}
/**
* Custom handler for Errors while parsing documents.
*/
private class Validator extends DefaultHandler {
public boolean validationError = false;
public SAXParseException saxParseException = null;
/**
* @throws SAXException
*/
@Override
public void error(SAXParseException exception) throws SAXException {
validationError = true;
saxParseException = exception;
}
/**
* @throws SAXException
*/
@Override
public void fatalError(SAXParseException exception) throws SAXException {
validationError = true;
saxParseException = exception;
}
/**
* @throws SAXException
*/
@Override
public void warning(SAXParseException exception) throws SAXException {
}
}
}
Constants.Java包含您需要指定的验证属性
public final class Constants {
public static final String VALIDATION_PROP = "http://xml.org/sax/features/validation";
public static final String SCHEMA_PROP = "http://apache.org/xml/features/validation/schema";
public static final String DYNAMIC_PROP = "http://apache.org/xml/features/validation/dynamic";
public static final String SCHEMA_CHECKING_PROP =
"http://apache.org/xml/features/validation/schema-full-checking";
public static final String SCHEMA_LOC =
"http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation";
public static final String VALID_XML_DOCUMENT = "The above XML is valid.";
public static final String INVALID_XML_DOCUMENT = "The Document has error at:";
}