本文整理汇总了Java中org.apache.xmlbeans.XmlObject.newCursor方法的典型用法代码示例。如果您正苦于以下问题:Java XmlObject.newCursor方法的具体用法?Java XmlObject.newCursor怎么用?Java XmlObject.newCursor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.xmlbeans.XmlObject
的用法示例。
在下文中一共展示了XmlObject.newCursor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createSample
import org.apache.xmlbeans.XmlObject; //导入方法依赖的package包/类
public String createSample( SchemaType sType )
{
XmlObject object = XmlObject.Factory.newInstance();
XmlCursor cursor = object.newCursor();
// Skip the document node
cursor.toNextToken();
// Using the type and the cursor, call the utility method to get a
// sample XML payload for that Schema element
createSampleForType( sType, cursor );
// Cursor now contains the sample payload
// Pretty print the result. Note that the cursor is positioned at the
// end of the doc so we use the original xml object that the cursor was
// created upon to do the xmlText() against.
cursor.dispose();
XmlOptions options = new XmlOptions();
options.put( XmlOptions.SAVE_PRETTY_PRINT );
options.put( XmlOptions.SAVE_PRETTY_PRINT_INDENT, 3 );
options.put( XmlOptions.SAVE_AGGRESSIVE_NAMESPACES );
options.setSaveOuter();
String result = object.xmlText( options );
return result;
}
示例2: createSampleForElement
import org.apache.xmlbeans.XmlObject; //导入方法依赖的package包/类
public static String createSampleForElement( SchemaGlobalElement element )
{
XmlObject xml = XmlObject.Factory.newInstance();
XmlCursor c = xml.newCursor();
c.toNextToken();
c.beginElement( element.getName() );
new SampleXmlUtil( false ).createSampleForType( element.getType(), c );
c.dispose();
XmlOptions options = new XmlOptions();
options.put( XmlOptions.SAVE_PRETTY_PRINT );
options.put( XmlOptions.SAVE_PRETTY_PRINT_INDENT, 3 );
options.put( XmlOptions.SAVE_AGGRESSIVE_NAMESPACES );
options.setSaveOuter();
String result = xml.xmlText( options );
return result;
}
示例3: escapeAttributeValue
import org.apache.xmlbeans.XmlObject; //导入方法依赖的package包/类
/**
* Escapes the reserved characters in a value of an attribute
*
* @param value Unescaped text
* @return The escaped text
*/
public String escapeAttributeValue(Object value)
{
String text = ScriptRuntime.toString(value);
if (text.length() == 0) return "";
XmlObject xo = XmlObject.Factory.newInstance();
XmlCursor cursor = xo.newCursor();
cursor.toNextToken();
cursor.beginElement("a");
cursor.insertAttributeWithValue("a", text);
cursor.dispose();
String elementText = xo.toString();
int begin = elementText.indexOf('"');
int end = elementText.lastIndexOf('"');
return elementText.substring(begin + 1, end);
}
示例4: createSampleForType
import org.apache.xmlbeans.XmlObject; //导入方法依赖的package包/类
public static String createSampleForType( SchemaType sType )
{
XmlObject object = XmlObject.Factory.newInstance();
XmlCursor cursor = object.newCursor();
// Skip the document node
cursor.toNextToken();
// Using the type and the cursor, call the utility method to get a
// sample XML payload for that Schema element
new SampleXmlUtil( false ).createSampleForType( sType, cursor );
// Cursor now contains the sample payload
// Pretty print the result. Note that the cursor is positioned at the
// end of the doc so we use the original xml object that the cursor was
// created upon to do the xmlText() against.
cursor.dispose();
XmlOptions options = new XmlOptions();
options.put( XmlOptions.SAVE_PRETTY_PRINT );
options.put( XmlOptions.SAVE_PRETTY_PRINT_INDENT, 3 );
options.put( XmlOptions.SAVE_AGGRESSIVE_NAMESPACES );
options.setSaveOuter();
String result = object.xmlText( options );
return result;
}
示例5: createTextElement
import org.apache.xmlbeans.XmlObject; //导入方法依赖的package包/类
/**
*
* @param qname
* @param value
* @return
*/
static XML createTextElement(XMLLibImpl lib, javax.xml.namespace.QName qname, String value)
{
XScriptAnnotation anno;
XmlObject xo = XmlObject.Factory.newInstance();
XmlCursor cursor = xo.newCursor();
try {
cursor.toNextToken();
cursor.beginElement(qname.getLocalPart(), qname.getNamespaceURI());
//if(namespace.length() > 0)
// cursor.insertNamespace("", namespace);
cursor.insertChars(value);
cursor.toStartDoc();
cursor.toNextToken();
anno = new XScriptAnnotation(cursor);
cursor.setBookmark(anno);
} finally {
cursor.dispose();
}
return new XML(lib, anno);
}
示例6: createFromXmlObject
import org.apache.xmlbeans.XmlObject; //导入方法依赖的package包/类
static XML createFromXmlObject(XMLLibImpl lib, XmlObject xo)
{
XScriptAnnotation anno;
XmlCursor curs = xo.newCursor();
if (curs.currentTokenType().isStartdoc())
{
curs.toFirstContentToken();
}
try {
anno = new XScriptAnnotation(curs);
curs.setBookmark(anno);
} finally {
curs.dispose();
}
return new XML(lib, anno);
}
示例7: escapeTextValue
import org.apache.xmlbeans.XmlObject; //导入方法依赖的package包/类
/**
* Escapes the reserved characters in a value of a text node
*
* @param value Unescaped text
* @return The escaped text
*/
public String escapeTextValue(Object value)
{
if (value instanceof XMLObjectImpl) {
return ((XMLObjectImpl)value).toXMLString(0);
}
String text = ScriptRuntime.toString(value);
if (text.length() == 0) return text;
XmlObject xo = XmlObject.Factory.newInstance();
XmlCursor cursor = xo.newCursor();
cursor.toNextToken();
cursor.beginElement("a");
cursor.insertChars(text);
cursor.dispose();
String elementText = xo.toString();
int begin = elementText.indexOf('>') + 1;
int end = elementText.lastIndexOf('<');
return (begin < end) ? elementText.substring(begin, end) : "";
}
示例8: addSection
import org.apache.xmlbeans.XmlObject; //导入方法依赖的package包/类
public XmlObjectBuilder addSection(String sectionName, XmlObject section){
cursor.beginElement(sectionName);
cursor.push();
XmlCursor srcCursor = section.newCursor();
srcCursor.toNextToken();
while(srcCursor.currentTokenType() != XmlCursor.TokenType.NONE && srcCursor.currentTokenType() != XmlCursor.TokenType.ENDDOC){
srcCursor.copyXml(cursor);
if(srcCursor.currentTokenType() == XmlCursor.TokenType.START) srcCursor.toEndToken();
srcCursor.toNextToken();
}
cursor.pop();
cursor.toEndToken();
cursor.toNextToken();
srcCursor.dispose();
return this;
}
示例9: addSection
import org.apache.xmlbeans.XmlObject; //导入方法依赖的package包/类
public XmlObjectBuilder addSection(String sectionName, XmlObject section) {
cursor.beginElement(sectionName);
cursor.push();
XmlCursor srcCursor = section.newCursor();
srcCursor.toNextToken();
while (srcCursor.currentTokenType() != XmlCursor.TokenType.NONE
&& srcCursor.currentTokenType() != XmlCursor.TokenType.ENDDOC) {
srcCursor.copyXml(cursor);
if (srcCursor.currentTokenType() == XmlCursor.TokenType.START)
srcCursor.toEndToken();
srcCursor.toNextToken();
}
cursor.pop();
cursor.toEndToken();
cursor.toNextToken();
srcCursor.dispose();
return this;
}
示例10: qualifySubstitutionGroup
import org.apache.xmlbeans.XmlObject; //导入方法依赖的package包/类
/**
* Qualifies a valid member of a substitution group. This method tries to use the
* built-in {@link XmlObject#substitute(QName, SchemaType)} and if succesful returns
* a valid substitution which is usable (not disconnected). If it fails, it uses
* low-level {@link XmlCursor} manipulation to qualify the substitution group. Note
* that if the latter is the case the resulting document is disconnected and should
* no longer be manipulated. Thus, use it as a final step after all markup is included.
*
* If newType is null, this method will skip {@link XmlObject#substitute(QName, SchemaType)}
* and directly use {@link XmlCursor}. This can be used, if you are sure that the substitute
* is not in the list of (pre-compiled) valid substitutions (this is the case if a schema
* uses another schema's type as a base for elements. E.g. om:Observation uses gml:_Feature
* as the base type).
*
* @param xobj
* the abstract element
* @param newInstance
* the new {@link QName} of the instance
* @param newType the new schemaType. if null, cursors will be used and the resulting object
* will be disconnected.
* @return if successful applied {@link XmlObject#substitute(QName, SchemaType)} a living object with a
* type == newType is returned. Otherwise null is returned as you can no longer manipulate the object.
*/
public static XmlObject qualifySubstitutionGroup(XmlObject xobj, QName newInstance, SchemaType newType) {
XmlObject substitute = null;
if (newType != null) {
substitute = xobj.substitute(newInstance, newType);
if (substitute != null && substitute.schemaType() == newType
&& substitute.getDomNode().getLocalName().equals(newInstance.getLocalPart()))
{
return substitute;
}
}
XmlCursor cursor = xobj.newCursor();
cursor.setName(newInstance);
QName qName = new QName("http://www.w3.org/2001/XMLSchema-instance", "type");
cursor.removeAttribute(qName);
cursor.toNextToken();
if (cursor.isNamespace()) {
cursor.removeXml();
}
cursor.dispose();
return null;
}
示例11: createEmptyXML
import org.apache.xmlbeans.XmlObject; //导入方法依赖的package包/类
static XML createEmptyXML(XMLLibImpl lib)
{
XScriptAnnotation anno;
XmlObject xo = XmlObject.Factory.newInstance();
XmlCursor curs = xo.newCursor();
try {
anno = new XScriptAnnotation(curs);
curs.setBookmark(anno);
} finally {
curs.dispose();
}
return new XML(lib, anno);
}
示例12: removeToken
import org.apache.xmlbeans.XmlObject; //导入方法依赖的package包/类
/**
*
* @param curs
*/
protected void removeToken (XmlCursor curs)
{
XmlObject xo = XmlObject.Factory.newInstance();
// Don't delete anything move to another document so it gets orphaned nicely.
XmlCursor tmpCurs = xo.newCursor();
tmpCurs.toFirstContentToken();
curs.moveXml(tmpCurs);
tmpCurs.dispose();
}
示例13: copy
import org.apache.xmlbeans.XmlObject; //导入方法依赖的package包/类
/**
*
* @param cursToCopy
* @return
*/
private XmlCursor copy (XmlCursor cursToCopy)
{
XmlObject xo = XmlObject.Factory.newInstance();
XmlCursor copyCurs = null;
if (cursToCopy.currentTokenType().isText())
{
try
{
// Try just as a textnode, to do that we need to wrap the text in a special fragment tag
// that is not visible from the XmlCursor.
copyCurs = XmlObject.Factory.parse("<x:fragment xmlns:x=\"http://www.openuri.org/fragment\">" +
cursToCopy.getChars() +
"</x:fragment>").newCursor();
if (!cursToCopy.toNextSibling())
{
if (cursToCopy.currentTokenType().isText())
{
cursToCopy.toNextToken(); // It's not an element it's text so skip it.
}
}
}
catch (Exception ex)
{
throw ScriptRuntime.typeError(ex.getMessage());
}
}
else
{
copyCurs = xo.newCursor();
copyCurs.toFirstContentToken();
if (cursToCopy.currentTokenType() == XmlCursor.TokenType.STARTDOC)
{
cursToCopy.toNextToken();
}
cursToCopy.copyXml(copyCurs);
if (!cursToCopy.toNextSibling()) // If element skip element.
{
if (cursToCopy.currentTokenType().isText())
{
cursToCopy.toNextToken(); // It's not an element it's text so skip it.
}
}
}
copyCurs.toStartDoc();
copyCurs.toFirstContentToken();
return copyCurs;
}
示例14: insertChild
import org.apache.xmlbeans.XmlObject; //导入方法依赖的package包/类
/**
*
* @param curs
* @param xmlToInsert
*/
private void insertChild(XmlCursor curs, Object xmlToInsert)
{
if (xmlToInsert == null || xmlToInsert instanceof Undefined)
{
// Do nothing
}
else if (xmlToInsert instanceof XmlCursor)
{
moveSrcToDest((XmlCursor)xmlToInsert, curs, true);
}
else if (xmlToInsert instanceof XML)
{
XML xmlValue = (XML) xmlToInsert;
// If it's an attribute, then change to text node
if (xmlValue.tokenType() == XmlCursor.TokenType.ATTR)
{
insertChild(curs, xmlValue.toString());
}
else
{
XmlCursor cursToInsert = ((XML) xmlToInsert).newCursor();
moveSrcToDest(cursToInsert, curs, true);
cursToInsert.dispose();
}
}
else if (xmlToInsert instanceof XMLList)
{
XMLList list = (XMLList) xmlToInsert;
for (int i = 0; i < list.length(); i++)
{
insertChild(curs, list.item(i));
}
}
else
{
// Convert to string and make XML out of it
String xmlStr = ScriptRuntime.toString(xmlToInsert);
XmlObject xo = XmlObject.Factory.newInstance(); // Create an empty document.
XmlCursor sourceCurs = xo.newCursor();
sourceCurs.toNextToken();
// To hold the text.
sourceCurs.insertChars(xmlStr);
sourceCurs.toPrevToken();
// Call us again with the cursor.
moveSrcToDest(sourceCurs, curs, true);
}
}
示例15: evaluate
import org.apache.xmlbeans.XmlObject; //导入方法依赖的package包/类
/**
* Using XPath or XQuery.
* NOTE!!!! To use this code, need to include
* xbean_xpath.jar, saxon9.jar, saxon9-dom.jar in CLASSPATH
* in startWebLogic.cmd
* @param xmlbean
* @param path
* @return
*/
public static String evaluate(XmlObject xmlbean, String path) {
XmlCursor cursor = xmlbean.newCursor();
String value;
// 1.2.3. use XQuery or selectPath
//
// cursor.toFirstChild();
// String defaultNamespace = cursor.namespaceForPrefix("");
// String namespaceDecl = "declare default element namespace '" + defaultNamespace + "';";
// Map<String,String> namespaces = new HashMap<String,String>();
// cursor.getAllNamespaces(namespaces);
// for (String prefix : namespaces.keySet())
// {
// namespaceDecl += "declare namespace " + prefix + "='" + namespaces.get(prefix) + "';";
// }
// 1. use XQuery
// XmlCursor results = cursor.execQuery(namespaceDecl + path);
// value = (results==null)?null:results.getTextValue();
// 2. use selectPath on XmlObject
// XmlObject[] result = xmlbean.selectPath(namespaceDecl + path);
// 3. use selectPath on XmlCursor
// cursor.toParent();
// XmlOptions options = new XmlOptions();
// options.put(Path.PATH_DELEGATE_INTERFACE,"org.apache.xmlbeans.impl.xpath.saxon.XBeansXPath");
// cursor.selectPath(namespaceDecl + path, options);
// if (cursor.getSelectionCount()>0) {
// cursor.toNextSelection();
// value = cursor.getTextValue();
// } else value = null;
// 4. use our own implementation
try {
XmlPath matcher = new XmlPath(path);
value = matcher.evaluate_segment(cursor, matcher.path_seg);
} catch (XmlException e) {
value = null; // xpath syntax error - treated as no match
}
cursor.dispose();
return value;
}