protected void testCopy(Element element) throws Exception { assertTrue("Not null", element != null); int attributeCount = element.attributeCount(); int nodeCount = element.nodeCount(); Element copy = element.createCopy(); assertEquals("Node size not equal after copy", element.nodeCount(), nodeCount); assertTrue("Same attribute size after copy", element.attributeCount() == attributeCount); assertTrue("Copy has same node size", copy.nodeCount() == nodeCount); assertTrue("Copy has same attribute size", copy.attributeCount() == attributeCount); for (int i = 0; i < attributeCount; i++) { Attribute attr1 = element.attribute(i); Attribute attr2 = copy.attribute(i); assertTrue("Attribute: " + i + " name is equal", attr1.getName().equals(attr2.getName())); assertTrue("Attribute: " + i + " value is equal", attr1.getValue().equals(attr2.getValue())); } for (int i = 0; i < nodeCount; i++) { Node node1 = element.node(i); Node node2 = copy.node(i); assertTrue("Node: " + i + " type is equal", node1.getNodeType() == node2.getNodeType()); assertTrue("Node: " + i + " value is equal", node1.getText().equals(node2.getText())); } }
public static List<TradingPriceTracking> getPriceTrackingItem(String res, String title) throws Exception { List<TradingPriceTracking> list = new ArrayList<TradingPriceTracking>(); Document document = formatStr2Doc(res); Element rootElt = document.getRootElement(); Element searchResult = rootElt.element("searchResult"); Iterator items = searchResult.elementIterator("item"); while (items.hasNext()) { TradingPriceTracking priceTracking = new TradingPriceTracking(); Element item = (Element) items.next(); priceTracking.setItemid(SamplePaseXml.getSpecifyElementText(item, "itemId")); priceTracking.setCategoryid( SamplePaseXml.getSpecifyElementText(item, "primaryCategory", "categoryId")); priceTracking.setCategoryname( SamplePaseXml.getSpecifyElementText(item, "primaryCategory", "categoryName")); priceTracking.setCurrentprice( SamplePaseXml.getSpecifyElementText(item, "sellingStatus", "currentPrice")); priceTracking.setSellerusername( SamplePaseXml.getSpecifyElementText(item, "sellerInfo", "sellerUserName")); priceTracking.setTitle(SamplePaseXml.getSpecifyElementText(item, "title")); priceTracking.setBidcount( SamplePaseXml.getSpecifyElementText(item, "sellingStatus", "bidCount")); priceTracking.setPictureurl(SamplePaseXml.getSpecifyElementText(item, "galleryURL")); String starttime = SamplePaseXml.getSpecifyElementText(item, "listingInfo", "startTime"); String endtime = SamplePaseXml.getSpecifyElementText(item, "listingInfo", "endTime"); if (StringUtils.isNotBlank(starttime)) { priceTracking.setStarttime(DateUtils.returnDate(starttime)); } if (StringUtils.isNotBlank(endtime)) { priceTracking.setEndtime(DateUtils.returnDate(endtime)); } Element sellingStatus = item.element("sellingStatus"); String currencyId1 = ""; if (sellingStatus != null) { Element currentPrice = sellingStatus.element("currentPrice"); if (currentPrice != null) { Attribute currencyId = currentPrice.attribute("currencyId"); if (currencyId != null) { currencyId1 = currencyId.getValue(); } } } priceTracking.setCurrencyid(currencyId1); Element shippingInfo = item.element("shippingInfo"); if (shippingInfo != null) { Element shippingServiceCost = shippingInfo.element("shippingServiceCost"); if (shippingServiceCost != null) { Attribute shippingcurrencyId = shippingServiceCost.attribute("currencyId"); if (shippingcurrencyId != null) { priceTracking.setShippingcurrencyid(shippingcurrencyId.getValue()); } priceTracking.setShippingservicecost(shippingServiceCost.getTextTrim()); } } priceTracking.setQuerytitle(title); list.add(priceTracking); } return list; }
// 解析价格跟踪 public static List<TradingPriceTracking> getPriceTrackingItemByItemId(String res) throws Exception { List<TradingPriceTracking> priceTrackings = new ArrayList<TradingPriceTracking>(); Document document = formatStr2Doc(res); Element rootElt = document.getRootElement(); Iterator items = rootElt.elementIterator("Item"); while (items.hasNext()) { TradingPriceTracking priceTracking = new TradingPriceTracking(); Element item = (Element) items.next(); priceTracking.setItemid(SamplePaseXml.getSpecifyElementText(item, "ItemID")); priceTracking.setTitle(SamplePaseXml.getSpecifyElementText(item, "Title")); priceTracking.setCurrentprice( SamplePaseXml.getSpecifyElementText(item, "ConvertedCurrentPrice")); priceTracking.setBidcount(SamplePaseXml.getSpecifyElementText(item, "BidCount")); Element ConvertedCurrentPrice = item.element("ConvertedCurrentPrice"); String endtime = SamplePaseXml.getSpecifyElementText(item, "EndTime"); if (StringUtils.isNotBlank(endtime)) { priceTracking.setEndtime(DateUtils.returnDate(endtime)); } String currencyId1 = ""; if (ConvertedCurrentPrice != null) { Attribute currencyId = ConvertedCurrentPrice.attribute("currencyId"); if (currencyId != null) { currencyId1 = currencyId.getValue(); } } priceTracking.setCurrencyid(currencyId1); priceTrackings.add(priceTracking); } return priceTrackings; }
/** Display a single cluster and its sub-clusters. */ @SuppressWarnings({"unchecked"}) private static void display(Element group, int level) { final int recursiveDocumentCount = Integer.parseInt(group.attribute("size").getValue()); final String label = group.element("title").elementText("phrase"); for (int i = 0; i < level; i++) System.out.print(" "); System.out.println(label + " [" + recursiveDocumentCount + " document(s)]"); final Iterator<Element> i = group.elementIterator("group"); while (i.hasNext()) { display(i.next(), level + 1); } }
/** * Set this element next in the tab order. Returns the element for easier nesting in expressions. */ protected Element setTabOrder(Element ele) { ele.attribute("tabindex", tabindex++); return ele; }
private static List<Node> doInsert( Node insertionNode, List<Node> clonedNodes, XFormsInstance modifiedInstance, boolean doDispatch) { final List<Node> insertedNodes = new ArrayList<Node>(clonedNodes.size()); if (insertionNode instanceof Element) { // Insert inside an element final Element insertContextElement = (Element) insertionNode; int otherNodeIndex = 0; for (Node clonedNode : clonedNodes) { if (clonedNode != null) { // NOTE: we allow passing some null nodes so we check on null if (clonedNode instanceof Attribute) { // Add attribute to element // NOTE: In XML, attributes are unordered. dom4j handles them as a list so has order, // but the // XForms spec shouldn't rely on attribute order. We could try to keep the order, but it // is harder // as we have to deal with removing duplicate attributes and find a reasonable insertion // strategy. final Attribute clonedAttribute = (Attribute) clonedNode; final Attribute existingAttribute = insertContextElement.attribute(clonedAttribute.getQName()); if (existingAttribute != null) insertContextElement.remove(existingAttribute); insertContextElement.add(clonedAttribute); if (existingAttribute != null) { // Dispatch xxforms-replace event if required and possible // NOTE: For now, still dispatch xforms-insert for backward compatibility. if (doDispatch && modifiedInstance != null) { final DocumentWrapper documentWrapper = (DocumentWrapper) modifiedInstance.documentInfo(); Dispatch.dispatchEvent( new XXFormsReplaceEvent( modifiedInstance, documentWrapper.wrap(existingAttribute), documentWrapper.wrap(clonedAttribute))); } } insertedNodes.add(clonedAttribute); } else if (!(clonedNode instanceof Document)) { // Add other node to element insertContextElement.content().add(otherNodeIndex++, clonedNode); insertedNodes.add(clonedNode); } else { // "If a cloned node cannot be placed at the target location due to a node type // conflict, then the // insertion for that particular clone node is ignored." } } } return insertedNodes; } else if (insertionNode instanceof Document) { final Document insertContextDocument = (Document) insertionNode; // "If there is more than one cloned node to insert, only the first node that does not cause a // conflict is // considered." for (Node clonedNode : clonedNodes) { // Only an element can be inserted at the root of an instance if (clonedNode instanceof Element) { final Element formerRootElement = insertContextDocument.getRootElement(); insertContextDocument.setRootElement((Element) clonedNode); // Dispatch xxforms-replace event if required and possible // NOTE: For now, still dispatch xforms-insert for backward compatibility. if (doDispatch && modifiedInstance != null) { final DocumentWrapper documentWrapper = (DocumentWrapper) modifiedInstance.documentInfo(); Dispatch.dispatchEvent( new XXFormsReplaceEvent( modifiedInstance, documentWrapper.wrap(formerRootElement), documentWrapper.wrap(insertContextDocument.getRootElement()))); } insertedNodes.add(clonedNode); return insertedNodes; } } // NOTE: The spec does not allow inserting comments and PIs at the root of an instance // document at this // point. return insertedNodes; } else { throw new OXFException( "Unsupported insertion node type: " + insertionNode.getClass().getName()); } }