@LibraryMethod(title="selectXMLValue", description="select values from a XML element", category="GF_EDIHELPERS", type=ExecutionType.ALL_VALUES_OF_CONTEXT)
public void selectXMLValue (
@Argument(title="node in return as XML") String[] xmllist,
@Parameter(title="field where the key is located") String keyfield,
@Parameter(title="comma seperated list of allowed values") String keyvaluelist,
@Parameter(title="if the key is found which field values should be returned.") String resultField,
ResultList result,
Container container) throws StreamTransformationException{
try{
StringBuffer sb = new StringBuffer();
for (String xml : xmllist) {
if(xml.trim().length()>0){
String[] keyvalues = keyvaluelist.split(",");
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document document = docBuilder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
NodeList tagslist = document.getElementsByTagName(keyfield);
for(int i = 0; i< tagslist.getLength();i++) {
Node node = tagslist.item(i);
String nodename = node.getFirstChild().getNodeValue();
for (String keyvalue : keyvalues) {
if(nodename.equals(keyvalue)){
//add the content of the result field to the output
//look from the parrent node
Element parrentNode = (Element)node.getParentNode();
NodeList resultList = parrentNode.getElementsByTagName(resultField);
for(int j = 0; j< resultList.getLength();j++) {
Node resultnode = resultList.item(j);
sb.append(resultnode.getFirstChild().getNodeValue());
}
}
}
}
}
}
result.addValue( sb.toString());
}catch(Exception e){
throw new StreamTransformationException("Unable to parse xml", e);
}
}
com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
9 | |
8 | |
6 | |
5 | |
4 | |
4 | |
4 | |
3 | |
3 | |
3 |