本文整理汇总了Java中javax.xml.stream.XMLStreamWriter.writeNamespace方法的典型用法代码示例。如果您正苦于以下问题:Java XMLStreamWriter.writeNamespace方法的具体用法?Java XMLStreamWriter.writeNamespace怎么用?Java XMLStreamWriter.writeNamespace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.stream.XMLStreamWriter
的用法示例。
在下文中一共展示了XMLStreamWriter.writeNamespace方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeXMLByStAX
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
public static void writeXMLByStAX() throws XMLStreamException, FileNotFoundException {
XMLOutputFactory factory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = factory.createXMLStreamWriter(new FileOutputStream("output.xml"));
writer.writeStartDocument();
writer.writeCharacters(" ");
writer.writeComment("testing comment");
writer.writeCharacters(" ");
writer.writeStartElement("catalogs");
writer.writeNamespace("myNS", "http://blog.csdn.net/Chinajash");
writer.writeAttribute("owner", "sina");
writer.writeCharacters(" ");
writer.writeStartElement("http://blog.csdn.net/Chinajash", "catalog");
writer.writeAttribute("id", "007");
writer.writeCharacters("Apparel");
// 写入catalog元素的结束标签
writer.writeEndElement();
// 写入catalogs元素的结束标签
writer.writeEndElement();
// 结束 XML 文档
writer.writeEndDocument();
writer.close();
System.out.println("ok");
}
示例2: writeTo
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
/**
* Default implementation that relies on {@link #writePayloadTo(XMLStreamWriter)}
*/
@Override
public void writeTo(XMLStreamWriter w) throws XMLStreamException {
String soapNsUri = soapVersion.nsUri;
w.writeStartDocument();
w.writeStartElement("S","Envelope",soapNsUri);
w.writeNamespace("S",soapNsUri);
if(hasHeaders()) {
w.writeStartElement("S","Header",soapNsUri);
MessageHeaders headers = getHeaders();
for (Header h : headers.asList()) {
h.writeTo(w);
}
w.writeEndElement();
}
// write the body
w.writeStartElement("S","Body",soapNsUri);
writePayloadTo(w);
w.writeEndElement();
w.writeEndElement();
w.writeEndDocument();
}
示例3: writeTo
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
public void writeTo(XMLStreamWriter w) throws XMLStreamException {
w.writeStartElement("", name.getLocalPart(), name.getNamespaceURI());
w.writeDefaultNamespace(name.getNamespaceURI());
if (mustUnderstand) {
//Writing the ns declaration conditionally checking in the NSContext breaks XWSS. as readHeader() adds ns declaration,
// where as writing alonf with the soap envelope does n't add it.
//Looks like they expect the readHeader() and writeTo() produce the same infoset, Need to understand their usage
//if(w.getNamespaceContext().getPrefix(soapVersion.nsUri) == null) {
w.writeNamespace("S", soapVersion.nsUri);
w.writeAttribute("S", soapVersion.nsUri, MUST_UNDERSTAND, getMustUnderstandLiteral(soapVersion));
// } else {
// w.writeAttribute(soapVersion.nsUri,MUST_UNDERSTAND, getMustUnderstandLiteral(soapVersion));
// }
}
w.writeCharacters(value);
w.writeEndElement();
}
示例4: setUndeclaredPrefix
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
/**
* sets undeclared prefixes on the writer
* @param prefix
* @param writer
* @throws XMLStreamException
*/
private static void setUndeclaredPrefix(String prefix, String readerURI, XMLStreamWriter writer) throws XMLStreamException {
String writerURI = null;
if (writer.getNamespaceContext() != null) {
writerURI = writer.getNamespaceContext().getNamespaceURI(prefix);
}
if (writerURI == null) {
writer.setPrefix(prefix, readerURI != null ? readerURI : "");
writer.writeNamespace(prefix, readerURI != null ? readerURI : "");
}
}
示例5: writeToBodyStart
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
public void writeToBodyStart(XMLStreamWriter w) throws XMLStreamException {
String soapNsUri = soapVersion.nsUri;
w.writeStartDocument();
w.writeStartElement("S","Envelope",soapNsUri);
w.writeNamespace("S",soapNsUri);
if(hasHeaders()) {
w.writeStartElement("S","Header",soapNsUri);
MessageHeaders headers = getHeaders();
for (Header h : headers.asList()) {
h.writeTo(w);
}
w.writeEndElement();
}
// write the body
w.writeStartElement("S","Body",soapNsUri);
}
示例6: producePureXMLLogoutRequest
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
private ByteArrayOutputStream producePureXMLLogoutRequest(String logoutUrl, String nameID, String format, String sessionIndex, String issuer, String issueInstant) throws XMLStreamException, UnsupportedEncodingException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLOutputFactory factory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = factory.createXMLStreamWriter(baos);
writer.writeStartElement("saml2p", "LogoutRequest", "urn:oasis:names:tc:SAML:2.0:protocol");
writer.writeNamespace("saml2p", "urn:oasis:names:tc:SAML:2.0:protocol");
writer.writeAttribute("ID", "_" + UUID.randomUUID().toString());
writer.writeAttribute("Version", "2.0");
writer.writeAttribute("Destination", logoutUrl);
writer.writeAttribute("IssueInstant", issueInstant + "Z");
writer.writeStartElement("saml2", "Issuer", "urn:oasis:names:tc:SAML:2.0:assertion");
writer.writeNamespace("saml2", "urn:oasis:names:tc:SAML:2.0:assertion");
writer.writeCharacters(issuer);
writer.writeEndElement();
writer.writeStartElement("saml", "NameID", "urn:oasis:names:tc:SAML:2.0:assertion");
writer.writeNamespace("saml", "urn:oasis:names:tc:SAML:2.0:assertion");
writer.writeAttribute("Format", format);
writer.writeCharacters(nameID);
writer.writeEndElement();
writer.writeStartElement("saml2p", "SessionIndex", "urn:oasis:names:tc:SAML:2.0:protocol");
writer.writeCharacters(sessionIndex);
writer.writeEndElement();
writer.writeEndElement();
writer.flush();
return baos;
}
示例7: writeInscopeNamespaces
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
private void writeInscopeNamespaces(XMLStreamWriter writer, Set<String> prefixSet) throws XMLStreamException {
for (Map.Entry<String, String> e : _buffer.getInscopeNamespaces().entrySet()) {
String key = fixNull(e.getKey());
// If the prefix is already written, do not write the prefix
if (!prefixSet.contains(key)) {
writer.writeNamespace(key, e.getValue());
}
}
}
示例8: encodeQName
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
public static String encodeQName(XMLStreamWriter writer, QName qname,
PrefixFactory prefixFactory)
{
// NOTE: Here it is assumed that we do not serialize using default
// namespace declarations and therefore that writer.getPrefix will
// never return ""
try {
String namespaceURI = qname.getNamespaceURI();
String localPart = qname.getLocalPart();
if (namespaceURI == null || namespaceURI.equals("")) {
return localPart;
}
else {
String prefix = writer.getPrefix(namespaceURI);
if (prefix == null) {
prefix = prefixFactory.getPrefix(namespaceURI);
writer.writeNamespace(prefix, namespaceURI);
}
return prefix + ":" + localPart;
}
}
catch (XMLStreamException e) {
throw new RuntimeException(e);
}
}
示例9: testDuplicateNSDeclaration
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
@Test
public void testDuplicateNSDeclaration() {
// expect only 1 Namespace Declaration
final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<ns1:foo" + " xmlns:ns1=\"http://example.com/\">" + "</ns1:foo>";
// have XMLOutputFactory repair Namespaces
XMLOutputFactory ofac = XMLOutputFactory.newInstance();
ofac.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, new Boolean(true));
// send output to a Stream
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
StreamResult sr = new StreamResult(buffer);
XMLStreamWriter w = null;
// write a duplicate Namespace Declaration
try {
w = ofac.createXMLStreamWriter(sr);
w.writeStartDocument();
w.writeStartElement("ns1", "foo", "http://example.com/");
w.writeNamespace("ns1", "http://example.com/");
w.writeNamespace("ns1", "http://example.com/");
w.writeEndElement();
w.writeEndDocument();
w.close();
} catch (XMLStreamException xmlStreamException) {
xmlStreamException.printStackTrace();
Assert.fail(xmlStreamException.toString());
}
// debugging output for humans
System.out.println();
System.out.println("actual: \"" + buffer.toString() + "\"");
System.out.println("expected: \"" + EXPECTED_OUTPUT + "\"");
// are results as expected?
Assert.assertEquals(EXPECTED_OUTPUT, buffer.toString());
}
示例10: writeNamespaceAttributes
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
private int writeNamespaceAttributes(int item, XMLStreamWriter writer, boolean collectPrefixes, Set<String> prefixSet) throws XMLStreamException {
do {
switch(getNIIState(item)){
case STATE_NAMESPACE_ATTRIBUTE:
// Undeclaration of default namespace
writer.writeDefaultNamespace("");
if (collectPrefixes) {
prefixSet.add("");
}
break;
case STATE_NAMESPACE_ATTRIBUTE_P:
// Undeclaration of namespace
// Declaration with prefix
String prefix = readStructureString();
writer.writeNamespace(prefix, "");
if (collectPrefixes) {
prefixSet.add(prefix);
}
break;
case STATE_NAMESPACE_ATTRIBUTE_P_U:
// Declaration with prefix
prefix = readStructureString();
writer.writeNamespace(prefix, readStructureString());
if (collectPrefixes) {
prefixSet.add(prefix);
}
break;
case STATE_NAMESPACE_ATTRIBUTE_U:
// Default declaration
writer.writeDefaultNamespace(readStructureString());
if (collectPrefixes) {
prefixSet.add("");
}
break;
}
readStructure();
item = peekStructure();
} while((item & TYPE_MASK) == T_NAMESPACE_ATTRIBUTE);
return item;
}
示例11: testCR6420953
import javax.xml.stream.XMLStreamWriter; //导入方法依赖的package包/类
@Test
public void testCR6420953() {
try {
XMLOutputFactory xof = XMLOutputFactory.newInstance();
StringWriter sw = new StringWriter();
XMLStreamWriter w = xof.createXMLStreamWriter(sw);
w.writeStartDocument();
w.writeStartElement("element");
w.writeDefaultNamespace(XML_CONTENT);
w.writeNamespace("prefix", XML_CONTENT);
w.writeAttribute("attribute", XML_CONTENT);
w.writeAttribute(XML_CONTENT, "attribute2", XML_CONTENT);
w.writeAttribute("prefix", XML_CONTENT, "attribute3", XML_CONTENT);
w.writeCharacters("\n");
w.writeCharacters(XML_CONTENT);
w.writeCharacters("\n");
w.writeCharacters(XML_CONTENT.toCharArray(), 0, XML_CONTENT.length());
w.writeCharacters("\n");
w.writeEndElement();
w.writeEndDocument();
w.flush();
System.out.println(sw);
// make sure that the generated XML parses
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.newDocumentBuilder().parse(new InputSource(new StringReader(sw.toString())));
} catch (XMLStreamException xmlStreamException) {
xmlStreamException.printStackTrace();
Assert.fail(xmlStreamException.toString());
} catch (SAXException saxException) {
saxException.printStackTrace();
Assert.fail(saxException.toString());
} catch (ParserConfigurationException parserConfigurationException) {
parserConfigurationException.printStackTrace();
Assert.fail(parserConfigurationException.toString());
} catch (IOException ioException) {
ioException.printStackTrace();
Assert.fail(ioException.toString());
}
}