本文整理汇总了Java中org.w3c.dom.ls.LSParser.parse方法的典型用法代码示例。如果您正苦于以下问题:Java LSParser.parse方法的具体用法?Java LSParser.parse怎么用?Java LSParser.parse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.w3c.dom.ls.LSParser
的用法示例。
在下文中一共展示了LSParser.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testExternalEncoding
import org.w3c.dom.ls.LSParser; //导入方法依赖的package包/类
@Test
public void testExternalEncoding() {
try {
LSInput src = null;
LSParser dp = null;
src = createLSInputEncoding();
dp = createLSParser();
src.setEncoding("UTF-16");
Document doc = dp.parse(src);
Assert.assertTrue("encodingXML".equals(doc.getDocumentElement().getNodeName()), "XML document is not parsed correctly");
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Exception occured: " + e.getMessage());
}
}
示例2: testLSInputParsingByteStream
import org.w3c.dom.ls.LSParser; //导入方法依赖的package包/类
@Test
public void testLSInputParsingByteStream() throws Exception {
DOMImplementationLS impl = (DOMImplementationLS) getDocumentBuilder().getDOMImplementation();
LSParser domParser = impl.createLSParser(MODE_SYNCHRONOUS, null);
LSInput src = impl.createLSInput();
try (InputStream is = new FileInputStream(ASTROCAT)) {
src.setByteStream(is);
assertNotNull(src.getByteStream());
// set certified accessor methods
boolean origCertified = src.getCertifiedText();
src.setCertifiedText(true);
assertTrue(src.getCertifiedText());
src.setCertifiedText(origCertified); // set back to orig
src.setSystemId(filenameToURL(ASTROCAT));
Document doc = domParser.parse(src);
Element result = doc.getDocumentElement();
assertEquals(result.getTagName(), "stardb");
}
}
示例3: testLSInputParsingString
import org.w3c.dom.ls.LSParser; //导入方法依赖的package包/类
@Test
public void testLSInputParsingString() throws Exception {
DOMImplementationLS impl = (DOMImplementationLS) getDocumentBuilder().getDOMImplementation();
String xml = "<?xml version='1.0'?><test>runDocumentLS_Q6</test>";
LSParser domParser = impl.createLSParser(MODE_SYNCHRONOUS, null);
LSSerializer domSerializer = impl.createLSSerializer();
// turn off xml decl in serialized string for comparison
domSerializer.getDomConfig().setParameter("xml-declaration", Boolean.FALSE);
LSInput src = impl.createLSInput();
src.setStringData(xml);
assertEquals(src.getStringData(), xml);
Document doc = domParser.parse(src);
String result = domSerializer.writeToString(doc);
assertEquals(result, "<test>runDocumentLS_Q6</test>");
}
示例4: normalizeXML
import org.w3c.dom.ls.LSParser; //导入方法依赖的package包/类
private static String normalizeXML(String xml) throws Exception {
// Remove all white space adjoining tags ("trim all elements")
xml = xml.replaceAll("\\s*<", "<");
xml = xml.replaceAll(">\\s*", ">");
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS domLS = (DOMImplementationLS) registry.getDOMImplementation("LS");
LSParser lsParser = domLS.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
LSInput input = domLS.createLSInput();
input.setStringData(xml);
Document document = lsParser.parse(input);
LSSerializer lsSerializer = domLS.createLSSerializer();
lsSerializer.getDomConfig().setParameter("comments", Boolean.FALSE);
lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
return lsSerializer.writeToString(document);
}
示例5: normalizeXML
import org.w3c.dom.ls.LSParser; //导入方法依赖的package包/类
/**
* Normalize and pretty-print XML so that it can be compared using string
* compare. The following code does the following: - Removes comments -
* Makes sure attributes are ordered consistently - Trims every element -
* Pretty print the document
*
* @param xml The XML to be normalized
* @return The equivalent XML, but now normalized
*/
public static String normalizeXML(String xml) throws Exception {
// Remove all white space adjoining tags ("trim all elements")
xml = xml.replaceAll("\\s*<", "<");
xml = xml.replaceAll(">\\s*", ">");
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS domLS = (DOMImplementationLS) registry.getDOMImplementation("LS");
LSParser lsParser = domLS.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
LSInput input = domLS.createLSInput();
input.setStringData(xml);
Document document = lsParser.parse(input);
LSSerializer lsSerializer = domLS.createLSSerializer();
lsSerializer.getDomConfig().setParameter("comments", Boolean.FALSE);
lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
return lsSerializer.writeToString(document);
}
示例6: testfilter0001
import org.w3c.dom.ls.LSParser; //导入方法依赖的package包/类
/**
* Equivalence class partitioning
* with state, input and output values orientation
* for public Document parse(LSInput is),
* <br><b>pre-conditions</b>: set filter that REJECTs any CHILD* node,
* <br><b>is</b>: xml1
* <br><b>output</b>: XML document with ELEMNENT1 and ELEMENT2 only.
*/
@Test
public void testfilter0001() {
LSParser parser = createLSParser();
if (parser == null) {
Assert.fail("Unable to create LSParser!");
}
// set filter
parser.setFilter(new LSParserFilter() {
public short startElement(Element elt) {
return FILTER_ACCEPT;
}
public short acceptNode(Node enode) {
if (enode.getNodeName().startsWith("CHILD")) {
return FILTER_REJECT;
}
return FILTER_ACCEPT;
}
public int getWhatToShow() {
return NodeFilter.SHOW_ALL;
}
});
String expected = "<?xml version=\"1.0\"?><ROOT><ELEMENT1></ELEMENT1><ELEMENT2>test1</ELEMENT2></ROOT>";
Document doc = parser.parse(getXmlSource(xml1));
if (!match(expected, doc)) {
Assert.fail("DOM structure after parsing is not equal to a structure of XML document, that being parsed");
}
System.out.println("OKAY");
}
示例7: match
import org.w3c.dom.ls.LSParser; //导入方法依赖的package包/类
public boolean match(String template, Node source) {
LSParser dp = createLSParser();
if (dp == null) {
System.out.println("Can not create LSParser.");
return false;
}
LSInput src = getXmlSource(template);
Document doc = dp.parse(src);
return checkXMLs(doc, source);
}
示例8: testFilter0002
import org.w3c.dom.ls.LSParser; //导入方法依赖的package包/类
/**
* Equivalence class partitioning with state, input and output values
* orientation for public Document parse(LSInput is), <br>
* <b>pre-conditions</b>: set filter that SKIPs ELEMENT1 node, <br>
* <b>is</b>: xml1 <br>
* <b>output</b>: XML document with CHILD1 and ELEMENT2 only.
*/
@Test
public void testFilter0002() {
LSParser parser = createLSParser();
if (parser == null) {
Assert.fail("Unable to create LSParser!");
}
// set filter
parser.setFilter(new LSParserFilter() {
public short startElement(Element elt) {
return FILTER_ACCEPT;
}
public short acceptNode(Node enode) {
if (enode.getNodeName().startsWith("ELEMENT1")) {
return FILTER_SKIP;
}
return FILTER_ACCEPT;
}
public int getWhatToShow() {
return NodeFilter.SHOW_ALL;
}
});
String expected = "<?xml version=\"1.0\"?><ROOT><CHILD1/><CHILD1><COC1/></CHILD1><ELEMENT2>test1<CHILD2/></ELEMENT2></ROOT>";
Document doc = parser.parse(getXmlSource(xml1));
if (!match(expected, doc)) {
Assert.fail("DOM structure after parsing is not equal to a structure of XML document, that being parsed");
}
System.out.println("OKAY");
}
示例9: testFilter0003
import org.w3c.dom.ls.LSParser; //导入方法依赖的package包/类
/**
* Equivalence class partitioning with state, input and output values
* orientation for public Document parse(LSInput is), <br>
* <b>pre-conditions</b>: set filter that SKIPs ELEMENT1 node, <br>
* <b>is</b>: xml1 <br>
* <b>output</b>: XML document with ELEMENT1 only.
*/
@Test
public void testFilter0003() {
LSParser parser = createLSParser();
if (parser == null) {
Assert.fail("Unable to create LSParser!");
}
// set filter
parser.setFilter(new LSParserFilter() {
public short startElement(Element elt) {
return FILTER_ACCEPT;
}
public short acceptNode(Node enode) {
if (enode.getNodeName().startsWith("ELEMENT2")) {
return FILTER_INTERRUPT;
}
return FILTER_ACCEPT;
}
public int getWhatToShow() {
return NodeFilter.SHOW_ALL;
}
});
String expected = "<ROOT><ELEMENT1><CHILD1/><CHILD1><COC1/></CHILD1></ELEMENT1></ROOT>";
Document doc = parser.parse(getXmlSource(xml1));
if (!match(expected, doc)) {
Assert.fail("DOM structure after parsing is not equal to a structure of XML document, that being parsed");
}
System.out.println("OKAY");
}
示例10: testFilter0004
import org.w3c.dom.ls.LSParser; //导入方法依赖的package包/类
/**
* Equivalence class partitioning with state, input and output values
* orientation for public Document parse(LSInput is), <br>
* <b>pre-conditions</b>: set filter that accepts all, <br>
* <b>is</b>: xml1 <br>
* <b>output</b>: full XML document.
*/
@Test
public void testFilter0004() {
LSParser parser = createLSParser();
if (parser == null) {
Assert.fail("Unable to create LSParser!");
}
// set filter
parser.setFilter(new LSParserFilter() {
public short startElement(Element elt) {
return FILTER_ACCEPT;
}
public short acceptNode(Node enode) {
return FILTER_ACCEPT;
}
public int getWhatToShow() {
return NodeFilter.SHOW_ALL;
}
});
String expected = "<ROOT><ELEMENT1><CHILD1/><CHILD1><COC1/></CHILD1></ELEMENT1><ELEMENT2>test1<CHILD2/></ELEMENT2></ROOT>";
Document doc = parser.parse(getXmlSource(xml1));
if (!match(expected, doc)) {
Assert.fail("DOM structure after parsing is not equal to a structure of XML document, that being parsed");
}
System.out.println("OKAY");
}
示例11: testFilter0005
import org.w3c.dom.ls.LSParser; //导入方法依赖的package包/类
/**
* Equivalence class partitioning with state, input and output values
* orientation for public Document parse(LSInput is), <br>
* <b>pre-conditions</b>: set filter that REJECTs all, <br>
* <b>is</b>: xml1 <br>
* <b>output</b>: empty XML document.
*/
@Test
public void testFilter0005() {
LSParser parser = createLSParser();
if (parser == null) {
Assert.fail("Unable to create LSParser!");
}
// set filter
parser.setFilter(new LSParserFilter() {
public short startElement(Element elt) {
return FILTER_ACCEPT;
}
public short acceptNode(Node enode) {
return FILTER_REJECT;
}
public int getWhatToShow() {
return NodeFilter.SHOW_ALL;
}
});
Document doc = parser.parse(getXmlSource(xml1));
NodeList children = doc.getDocumentElement().getChildNodes();
if (children.getLength() != 0) {
Assert.fail("Not all children skipped");
}
System.out.println("OKAY");
}
示例12: testFilter0006
import org.w3c.dom.ls.LSParser; //导入方法依赖的package包/类
/**
* Equivalence class partitioning with state, input and output values
* orientation for public Document parse(LSInput is), <br>
* <b>pre-conditions</b>: set filter that SKIPs all, <br>
* <b>is</b>: xml1 <br>
* <b>output</b>: empty XML document.
*/
@Test
public void testFilter0006() {
LSParser parser = createLSParser();
if (parser == null) {
Assert.fail("Unable to create LSParser!");
}
// set filter
parser.setFilter(new LSParserFilter() {
public short startElement(Element elt) {
return FILTER_ACCEPT;
}
public short acceptNode(Node enode) {
return FILTER_SKIP;
}
public int getWhatToShow() {
return NodeFilter.SHOW_ALL;
}
});
Document doc = parser.parse(getXmlSource(xml1));
NodeList children = doc.getDocumentElement().getChildNodes();
if (children.getLength() != 0) {
Assert.fail("Not all children skipped");
}
System.out.println("OKAY");
}
示例13: testFilter0007
import org.w3c.dom.ls.LSParser; //导入方法依赖的package包/类
/**
* Equivalence class partitioning with state, input and output values
* orientation for public Document parse(LSInput is), <br>
* <b>pre-conditions</b>: set filter that REJECTs any CHILD* start element, <br>
* <b>is</b>: xml1 <br>
* <b>output</b>: XML document with ELEMENT1 and ELEMENT2 only.
*/
@Test
public void testFilter0007() {
LSParser parser = createLSParser();
if (parser == null) {
Assert.fail("Unable to create LSParser!");
}
// set filter
parser.setFilter(new LSParserFilter() {
public short startElement(Element elt) {
if (elt.getTagName().startsWith("CHILD")) {
return FILTER_REJECT;
}
return FILTER_ACCEPT;
}
public short acceptNode(Node enode) {
return FILTER_ACCEPT;
}
public int getWhatToShow() {
return NodeFilter.SHOW_ALL;
}
});
String expected = "<?xml version=\"1.0\"?><ROOT><ELEMENT1></ELEMENT1><ELEMENT2>test1</ELEMENT2></ROOT>";
Document doc = parser.parse(getXmlSource(xml1));
if (!match(expected, doc)) {
Assert.fail("DOM structure after parsing is not equal to a structure of XML document, that being parsed");
}
System.out.println("OKAY");
}
示例14: testFilter0008
import org.w3c.dom.ls.LSParser; //导入方法依赖的package包/类
/**
* Equivalence class partitioning with state, input and output values
* orientation for public Document parse(LSInput is), <br>
* <b>pre-conditions</b>: set filter that SKIPs ELEMENT1 start element, <br>
* <b>is</b>: xml1 <br>
* <b>output</b>: XML document with CHILD1 and ELEMENT2 only.
*/
@Test
public void testFilter0008() {
LSParser parser = createLSParser();
if (parser == null) {
Assert.fail("Unable to create LSParser!");
}
// set filter
parser.setFilter(new LSParserFilter() {
public short startElement(Element elt) {
if (elt.getTagName().equals("ELEMENT1")) {
return FILTER_SKIP;
}
return FILTER_ACCEPT;
}
public short acceptNode(Node enode) {
return FILTER_ACCEPT;
}
public int getWhatToShow() {
return NodeFilter.SHOW_ALL;
}
});
String expected = "<?xml version=\"1.0\"?><ROOT><CHILD1/><CHILD1><COC1/></CHILD1><ELEMENT2>test1<CHILD2/></ELEMENT2></ROOT>";
Document doc = parser.parse(getXmlSource(xml1));
if (!match(expected, doc)) {
Assert.fail("DOM structure after parsing is not equal to a structure of XML document, that being parsed");
}
System.out.println("OKAY");
}
示例15: testFilter0009
import org.w3c.dom.ls.LSParser; //导入方法依赖的package包/类
/**
* Equivalence class partitioning with state, input and output values
* orientation for public Document parse(LSInput is), <br>
* <b>pre-conditions</b>: set filter that SKIPs ELEMENT1 start element, <br>
* <b>is</b>: xml1 <br>
* <b>output</b>: XML document with ELEMENT1 only.
*/
@Test
public void testFilter0009() {
LSParser parser = createLSParser();
if (parser == null) {
Assert.fail("Unable to create LSParser!");
}
// set filter
parser.setFilter(new LSParserFilter() {
public short startElement(Element elt) {
if (elt.getTagName().startsWith("ELEMENT2")) {
return FILTER_INTERRUPT;
}
return FILTER_ACCEPT;
}
public short acceptNode(Node enode) {
return FILTER_ACCEPT;
}
public int getWhatToShow() {
return NodeFilter.SHOW_ALL;
}
});
String expected = "<ROOT><ELEMENT1><CHILD1/><CHILD1><COC1/></CHILD1></ELEMENT1></ROOT>";
Document doc = parser.parse(getXmlSource(xml1));
if (!match(expected, doc)) {
Assert.fail("DOM structure after parsing is not equal to a structure of XML document, that being parsed");
}
System.out.println("OKAY");
}