Java Code Examples for org.jdom.Element#getTextNormalize()

The following examples show how to use org.jdom.Element#getTextNormalize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XMLUtil.java    From Shop-for-JavaWeb with MIT License 6 votes vote down vote up
/**
 * 获取子结点的xml
 * @param children
 * @return String
 */
public static String getChildrenText(List children) {
	StringBuffer sb = new StringBuffer();
	if(!children.isEmpty()) {
		Iterator it = children.iterator();
		while(it.hasNext()) {
			Element e = (Element) it.next();
			String name = e.getName();
			String value = e.getTextNormalize();
			List list = e.getChildren();
			sb.append("<" + name + ">");
			if(!list.isEmpty()) {
				sb.append(XMLUtil.getChildrenText(list));
			}
			sb.append(value);
			sb.append("</" + name + ">");
		}
	}
	
	return sb.toString();
}
 
Example 2
Source File: XMLUtil.java    From phone with Apache License 2.0 6 votes vote down vote up
/**
 * 获取子结点的xml
 * 
 * @param children
 * @return String
 */
@SuppressWarnings("rawtypes")
public static String getChildrenText(List children) {
	StringBuffer sb = new StringBuffer();
	if (!children.isEmpty()) {
		Iterator it = children.iterator();
		while (it.hasNext()) {
			Element e = (Element) it.next();
			String name = e.getName();
			String value = e.getTextNormalize();
			List list = e.getChildren();
			sb.append("<" + name + ">");
			if (!list.isEmpty()) {
				sb.append(XMLUtil.getChildrenText(list));
			}
			sb.append(value);
			sb.append("</" + name + ">");
		}
	}

	return sb.toString();
}
 
Example 3
Source File: XMLUtil.java    From Shop-for-JavaWeb with MIT License 5 votes vote down vote up
/**
 * 解析xml,返回第一级元素键值对。如果第一级元素有子节点,则此节点的值是子节点的xml数据。
 * @param strxml
 * @return
 * @throws JDOMException
 * @throws IOException
 */
public static Map doXMLParse(String strxml) throws JDOMException, IOException {
	strxml = strxml.replaceFirst("encoding=\".*\"", "encoding=\"UTF-8\"");

	if(null == strxml || "".equals(strxml)) {
		return null;
	}
	
	Map m = new HashMap();
	
	InputStream in = new ByteArrayInputStream(strxml.getBytes("UTF-8"));
	SAXBuilder builder = new SAXBuilder();
	Document doc = builder.build(in);
	Element root = doc.getRootElement();
	List list = root.getChildren();
	Iterator it = list.iterator();
	while(it.hasNext()) {
		Element e = (Element) it.next();
		String k = e.getName();
		String v = "";
		List children = e.getChildren();
		if(children.isEmpty()) {
			v = e.getTextNormalize();
		} else {
			v = XMLUtil.getChildrenText(children);
		}
		
		m.put(k, v);
	}
	
	//关闭流
	in.close();
	
	return m;
}
 
Example 4
Source File: XMLUtil.java    From phone with Apache License 2.0 5 votes vote down vote up
/**
 * 解析xml,返回第一级元素键值对。如果第一级元素有子节点,则此节点的值是子节点的xml数据。
 * 
 * @param strxml
 * @return
 * @throws JDOMException
 * @throws IOException
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static Map doXMLParse(String strxml) throws JDOMException, IOException {
	strxml = strxml.replaceFirst("encoding=\".*\"", "encoding=\"UTF-8\"");

	if (null == strxml || "".equals(strxml)) {
		return null;
	}
	Map m = new HashMap();
	InputStream in = new ByteArrayInputStream(strxml.getBytes(SystemConfig.CHARSET));
	SAXBuilder builder = new SAXBuilder();
	Document doc = builder.build(in);
	Element root = doc.getRootElement();
	List list = root.getChildren();
	Iterator it = list.iterator();
	while (it.hasNext()) {
		Element e = (Element) it.next();
		String k = e.getName();
		String v = "";
		List children = e.getChildren();
		if (children.isEmpty()) {
			v = e.getTextNormalize();
		} else {
			v = XMLUtil.getChildrenText(children);
		}

		m.put(k, v);
	}

	// 关闭流
	in.close();

	return m;
}
 
Example 5
Source File: AnnotationRulesFactoryImpl.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private String getElementValue(Element element, String defaultValue) {
	if (element == null) {
		return defaultValue;
	}
	String value = element.getTextNormalize();
	return value;
}
 
Example 6
Source File: AnnotationRulesFactoryImpl.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void loadRegex(Document doc) {
		try{
			XPath regexRule = XPath.newInstance("//implementation/script[@language='regex']");			
			List<?> regexRules = regexRule.selectNodes(doc);

			for (Object obj : regexRules) {
				Element scriptElement = (Element) obj;

				String regex = scriptElement.getTextNormalize();
				boolean isCaseInsensitive = regex.endsWith("/i");

				//java doesn't like the /^ switch so it is replaced by ^
				regex = regex.replace("/^", "^");
				//java does not support the /i case in-sensitivity switch so it is removed
				regex = regex.replace("/i", "");

				final Element ruleElement = scriptElement.getParentElement().getParentElement().getParentElement();
				
				final String id = getElementValue(ruleElement.getChild("id"), "");
				final String title = getElementValue(ruleElement.getChild("title"), "");
				final String description = getElementValue(ruleElement.getChild("description"), null);
				
				String status = null;
				Date date = null;
				final Element statusElement = ruleElement.getChild("status");
				if (statusElement != null) {
					status = statusElement.getTextNormalize();
					String dateString = statusElement.getAttributeValue("date", (String) null);
					if (dateString != null) {
						try {
							date = status_df.get().parse(dateString);
						} catch (ParseException e) {
							LOG.warn("Could not parse String as status date: "+dateString, e);
						}
					}
				}
				
				// TODO add parsing for grand fathering date
//				Date grandFatheringDate = null;
				

				Pattern pattern;

				if (isCaseInsensitive) {
					pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
				} else {
					pattern = Pattern.compile(regex);
				}

				AnnotationRegularExpressionFromXMLRule rule = new AnnotationRegularExpressionFromXMLRule();
				rule.setRuleId(id);
				rule.setName(title);
				rule.setDescription(description);
				rule.setStatus(status);
				rule.setDate(date);
//				rule.setGrandFatheringDate(grandFatheringDate);
				rule.setErrorMessage(title);
				rule.setPattern(pattern);
				rule.setRegex(regex);

				annotationRules.add(rule);
			}
		}catch(Exception ex){
			LOG.error(ex.getMessage(), ex);
		}
	}