类org.w3c.dom.traversal.DocumentTraversal源码实例Demo

下面列出了怎么用org.w3c.dom.traversal.DocumentTraversal的API类实例代码及写法,或者点击链接到github查看源代码。

/**
 * Transfers the document to a map.
 *
 * @param doc to be transfered to a map.
 * @return transfered map.
 */
private Map<String, String> docToMap(final Document doc) {
    final Map<String, String> map = new HashMap<>();

    final DocumentTraversal traversal = (DocumentTraversal) doc;
    final NodeIterator iterator = traversal.createNodeIterator(doc.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null, true);

    for (Node node = iterator.nextNode(); node != null; node = iterator.nextNode()) {
        final String name = ((Element) node).getTagName();
        final StringBuilder content = new StringBuilder();
        final NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            final Node child = children.item(i);
            if (child.getNodeType() == Node.TEXT_NODE) {
                content.append(child.getTextContent());
            }
        }
        if (!content.toString().trim().isEmpty()) {
            map.put(name, content.toString());
        }
    }
    return map;
}
 
源代码2 项目: jStyleParser   文件: AnalyzerTest.java
@BeforeClass
public static void init() throws IOException, CSSException, SAXException {
	log.info("\n\n\n == AnalyzerTest test at {} == \n\n\n", new Date());

       DOMSource ds = new DOMSource(AnalyzerTest.class.getResourceAsStream("/simple/data.html"));
       doc = ds.parse();
       
	sheet = CSSFactory.parse(AnalyzerTest.class.getResource("/simple/data.css"), null);

	analyzer = new Analyzer(sheet);

	NodeList list = doc.getElementsByTagName("body");
	assertEquals("There is one <body> element", 1, list.getLength());

	//walker = new TidyTreeWalker(list.item(0), NodeFilter.SHOW_ELEMENT);
	DocumentTraversal traversal = (DocumentTraversal) doc;
	walker = traversal.createTreeWalker(list.item(0), NodeFilter.SHOW_ELEMENT, null, false);
	elements = new ElementMap(doc);
}
 
源代码3 项目: container   文件: MBUtils.java
/**
 * Transfers the properties document to a map.
 *
 * @param propertiesDocument to be transfered to a map.
 * @return transfered map.
 */
public static HashMap<String, String> docToMap(final Document propertiesDocument, final boolean allowEmptyEntries) {
    final HashMap<String, String> reponseMap = new HashMap<>();

    final DocumentTraversal traversal = (DocumentTraversal) propertiesDocument;
    final NodeIterator iterator =
        traversal.createNodeIterator(propertiesDocument.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null, true);

    for (Node node = iterator.nextNode(); node != null; node = iterator.nextNode()) {

        final String name = ((Element) node).getLocalName();
        final StringBuilder content = new StringBuilder();
        final NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            final Node child = children.item(i);
            if (child.getNodeType() == Node.TEXT_NODE) {
                content.append(child.getTextContent());
            }
        }

        if (allowEmptyEntries) {
            reponseMap.put(name, content.toString());
        } else {
            if (!content.toString().trim().isEmpty()) {
                reponseMap.put(name, content.toString());
            }
        }
    }

    return reponseMap;
}
 
源代码4 项目: xmlunit   文件: NodeTest.java
/**
 * Try to cast a Document into a DocumentTraversal
 * @param document
 * @return DocumentTraversal interface if the DOM implementation supports it
 */
private static DocumentTraversal getDocumentTraversal(Document document) {
    try {
        return (DocumentTraversal) document;
    } catch (ClassCastException e) {
        throw new IllegalArgumentException("DOM Traversal not supported by "
                                           + document.getImplementation().getClass().getName()
                                           + ". To use this class you will need to switch to a DOM implementation that supports Traversal.");
    }
}
 
源代码5 项目: jStyleParser   文件: Traversal.java
public Traversal(Document doc, Object source, int whatToShow) {
    if (doc instanceof DocumentTraversal) {
        DocumentTraversal dt = (DocumentTraversal) doc;
        this.walker = dt.createTreeWalker(doc.getDocumentElement(), whatToShow, null, false);
    } else {
        this.walker = new GenericTreeWalker(doc.getDocumentElement(), whatToShow);
    }
    this.source = source;
}
 
源代码6 项目: jStyleParser   文件: ElementMap.java
public ElementMap(Document doc) {
	elementIDs = new HashMap<String, Element>();
	elementNames = new HashMap<String, Element>();
       DocumentTraversal traversal = (DocumentTraversal) doc;
       TreeWalker w = traversal.createTreeWalker(doc, NodeFilter.SHOW_ELEMENT, null, false);
	//TreeWalker w = new TidyTreeWalker(doc, NodeFilter.SHOW_ELEMENT);
	Element current;
	while ((current = (Element) w.nextNode()) != null) {
		elementNames.put(current.getNodeName().toLowerCase(), current);
		String id = current.getAttribute("id");
		if(id!=null)
			elementIDs.put(id, current);
	}
}
 
源代码7 项目: xmlunit   文件: NodeTest.java
/**
 * Construct a NodeTest using the specified DocumentTraversal, starting at
 * the specified root node
 */
public NodeTest(DocumentTraversal documentTraversal, Node rootNode) {
    this.documentTraversal = documentTraversal;
    this.rootNode = rootNode;
}
 
源代码8 项目: apogen   文件: UtilsClustering.java
private static LblTree getDomTree(String dom1) throws IOException {

		org.w3c.dom.Document doc1 = DomUtils.asDocument(dom1);

		LblTree domTree = null;

		DocumentTraversal traversal = (DocumentTraversal) doc1;
		TreeWalker walker = traversal.createTreeWalker(doc1.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null, true);
		domTree = createTree(walker);

		return domTree;
	}
 
 类所在包
 类方法
 同包方法