Aller au contenu principal

Über API Formulardefinition eines Feldes auslesen

Commentaires

1 commentaire

  • Zendesk API User
    Author: mhenke - 10/21/2013 11:07

    Ich habe es nun selbst mit folgender Klasse gelöst:

    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    import java.io.StringWriter;

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.transform.OutputKeys;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerException;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathConstants;
    import javax.xml.xpath.XPathExpression;
    import javax.xml.xpath.XPathExpressionException;
    import javax.xml.xpath.XPathFactory;

    import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;

    import de.espirit.firstspirit.access.store.templatestore.GomSourceProvider;

    public class FormParser {
         private Document document;

         private static DocumentBuilderFactory documentFactory;
         private static XPathFactory xPathFactory;
         private static TransformerFactory transformerFactory;
         static {
              documentFactory = DocumentBuilderFactory.newInstance();
              xPathFactory = XPathFactory.newInstance();
              transformerFactory = TransformerFactory.newInstance();
         }

         public FormParser(GomSourceProvider gsp) throws ParserConfigurationException, SAXException, IOException {
              String source = gsp.getGomSource();
              DocumentBuilder builder = documentFactory.newDocumentBuilder();
              document = builder.parse(new ByteArrayInputStream(source.getBytes()));
         }

         public String getFieldDefinition(String field) throws XPathExpressionException, TransformerException {
              XPath xPath = xPathFactory.newXPath();
              XPathExpression expr = xPath.compile("//*[parent::CMS_MODULE|parent::CMS_GROUP and @name='" + field + "']");
              NodeList nl = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
              if (nl.getLength() == 0) {
                   return null;
              }
              return nodeToString(nl.item(0));
         }

         private String nodeToString(Node node) throws TransformerException {
              StringWriter sw = new StringWriter();
              Transformer t = transformerFactory.newTransformer();
              t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
              t.setOutputProperty(OutputKeys.INDENT, "yes");
              t.transform(new DOMSource(node), new StreamResult(sw));
              return sw.toString();
         }
    }
    0

Vous devez vous connecter pour laisser un commentaire.