使用Xstream将xml作为String和Parsed值而不是Java类获取

时间:2016-10-28 00:57:04

标签: java xstream

你能帮忙!!

我已将get xml as string instead of class with xstream解决方案用于我的工作,但我只能获取Xml字符串。我需要获取xml字符串以及其他解析值,如下所示。

输入xml:

<data>
<EventHandler-In>
<request<processType>ABC</processType></request>
</EventHandler-In>
<requestNo>A12345</requestNo>
</data>

输出:

EventHandler_In = <EventHandler-In><request<processType>ABC</processType></request></EventHandler-In>

requestNo = "A12345"

代码从这里开始:

@XStreamAlias("data")

@XStreamConverter(value = DataXmlStringConverter.class)

public class PendingCaseInput
{

    private String EventHandler_In;

    @XStreamAlias("requestNo")
    private String requestNo;  

    public String getRequestNo()
    {
        return requestNo;
    }

    public void setRequestNo(String requestNo)
    {
        this.requestNo = requestNo;
    }

    public PendingCaseInput()
    {}


    public String getEventHandler_In()
    {
        return EventHandler_In;
    }
    public void setEventHandler_In(String eventHandler_In)
    {
        EventHandler_In = eventHandler_In;
    }

    public static void main(String[] args)
    {
        final XStream xstream = new XStream(new DomDriver());
        xstream.processAnnotations(PendingCaseInput.class);

        final String xml = "<data><EventHandler-In>           <request<processType>ABC</processType></request></EventHandler-In><requestNo>A12345</requestNo></data>";  

        final PendingCaseInput parent = (PendingCaseInput) xstream.fromXML(xml);
        System.out.println(parent.getEventHandler_In());
        System.out.println(parent.getRequestNo());
    }
}


 public class DataXmlStringConverter implements Converter
 {

     @Override

     public boolean canConvert(@SuppressWarnings("rawtypes") final Class type)
     {
         return PendingCaseInput.class.isAssignableFrom(type);
     }

@Override
public void marshal(Object source, HierarchicalStreamWriter writer,  MarshallingContext context)
{
     throw new UnsupportedOperationException("unmarshaling only");
}

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context)
{
     reader.moveDown();

     if (!"EventHandler-In".equals(reader.getNodeName()))
     {
         throw new ConversionException("Expected message, but was " +  reader.getNodeName());
     }
     final StringBuilder message = new StringBuilder();

     buildRecursiveMessage(reader, message);
     while (reader.hasMoreChildren())
     {
         reader.moveDown();
         buildRecursiveMessage(reader, message);
         reader.moveUp();
     }
     reader.moveUp();

     final PendingCaseInput inputData = new PendingCaseInput();
     inputData.setEventHandler_In(message.toString());
     return inputData;

}

 private void buildRecursiveMessage(final HierarchicalStreamReader      reader, final StringBuilder sb)
 {
    // Build start-tag
    final String nodeName = reader.getNodeName();
    sb.append("<" + nodeName);

    // Build attributes
    final int numAttributes = reader.getAttributeCount();
    if (numAttributes > 0)
    {
        sb.append(" ");
        for (int i = 0; i < numAttributes; i++)
        {
             final String attributeName = reader.getAttributeName(i);
             final String attributeValue = reader.getAttribute(i);
             sb.append(attributeName + "=\"" + attributeValue + "\"");

             final boolean lastAttribute = (i == numAttributes - 1);
             if (!lastAttribute)
             {
                 sb.append(", ");
             }
         }
    }

    // Build children
    final boolean containsChildren = reader.hasMoreChildren();
    final boolean containsValue = !reader.getValue().isEmpty();
    final boolean empty = !containsChildren && !containsValue;

    sb.append(!empty ? ">" : " />");

    if (containsChildren)
    {
        while (reader.hasMoreChildren())
        {
            reader.moveDown();
            buildRecursiveMessage(reader, sb);
            reader.moveUp();
        }
    }
    else if (containsValue)
    {
        sb.append(reader.getValue());
    }

    // Build end-tag
    if (!empty)
    {
         sb.append("</" + nodeName + ">");
    }
    }
}

0 个答案:

没有答案