从Web服务Java返回XML格式的结果

时间:2015-08-19 08:00:34

标签: java xml web-services

我的webservice中有这个简单的格式化程序类

 public abstract class BaseFormatter
{

    protected static final String XML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>";
    protected static final String ERROR = "error";
    protected static final String RESULT = "result";

    public static final String EMPTY = XML + "<results/>";

    protected StringBuilder sb = null;  

    protected void openTag(String tag)
    {
        sb.append("<");
        sb.append(tag);
        sb.append(">");
    }

    protected void openTagWithAttr(String tag, String attr, String val)
    {
        sb.append("<");
        sb.append(tag);
        if (val.length()>0)
        {
            sb.append(" ");
            sb.append(attr);
            sb.append("=\"");
            sb.append(val);
            sb.append("\"");
        }
        sb.append(">");
    }

    protected void closeTag(String tag)
    {
        sb.append("</");
        sb.append(tag);
        sb.append(">");
    }

/**
Returns an error document
**/

    public String getError(String msg)
    {
        sb = new StringBuilder();
        sb.append(XML);
        openTag(ERROR);
        sb.append(msg);
        closeTag(ERROR);
        return sb.toString();
    }

    private void addEmpty(String tag)
    {
        sb.append("<");
        sb.append(tag);
        sb.append("/>");
    }

/**
Adds elements to the document; NOTE: adds an empty element even if the value list is empty
**/

    protected void addElement(List<String> vals, List<String> keys)
    {
        int len = vals.size();
        openTag(RESULT);
        if (len == 0 && keys.size() > 0) addEmpty(keys.get(0));
        for (int i=0; i < len; i++)
        {
            String key = keys.get(i);
            openTag(key);
            sb.append(vals.get(i));
            closeTag(key);
        }
        closeTag(RESULT);
    }

BaseFormatter类

 private SimpleFormatter formatter = null;


      public String getJobData(Map<String, String> paramMap)
  {
    String str1 = (String)paramMap.get("jobId");
    if (str1 == null) {
      return this.formatter.getError("No job ID provided for query");
    }
    Query_filter localQuery_filter = new Query_filter("Job", str1);
    Query_filter[] arrayOfQuery_filter = { localQuery_filter };
    Node localNode = mgr.getQueryResult("HolderJob", JOB_NAMES, JOB_TAGS, arrayOfQuery_filter);
    if (localNode == null) {
      return this.formatter.getError("Could not retrieve job data; check job ID");
    }
    String str2 = null;
    try
    {
      str2 = Documents.writeToString(localNode);
    }
    catch (Exception localException) {}
    return this.formatter.getXml(str2);
  }

} 并且我在使用Web服务时使用它来格式化结果

return str2;

使用formatter类时,我得到一个空指针异常。

但如果我这样做:

<Table><Claimable>false</Claimable><MinorRev>150305</MinorRev><Operation>510190 ROW VERI</Operation><Experiment>219254</Experiment><HTNum>591</HTNum><WaferEC>150110</WaferEC></Table>

我得到的结果只是一个简单的字符串:

false150305510190 ROW VERI219254591150110

如果我使用格式化程序类,这是我期待的结果。

 <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes" omit-xml-declaration="no"  method="xml" ></xsl:output>
    <xsl:template match="/">
        <xsl:apply-templates select="//*/paragraph" />
    </xsl:template>

    <xsl:template match="paragraph" xml:space="default">
        <xsl:element name="Paragraph">
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>

    <xsl:template match="paragraph/*" xml:space="default">
        <xsl:attribute name="{name(.)}"><xsl:value-of select="."/></xsl:attribute>
    </xsl:template>
</xsl:stylesheet>

我正在使用Netbeans 8.0.2

Apache 7.0.63

任何人都可以指导我吗?

谢谢。

0 个答案:

没有答案