我想使用Java从以下xml中删除根元素:
<BookRequest xsi:schemaLocation="http://test/books.xsd">
<Book>
<Name>abc</Name>
<Author>xyz</Author>
</Book>
</BookRequest>
所以我只将它作为最终输出:
<Book>
<Name>abc</Name>
<Author>xyz</Author>
</Book>
我该怎么做?
答案 0 :(得分:0)
import java.util.Map;
import cjm.component.cb.map.ToMap;
import cjm.component.cb.xml.ToXML;
public class Convert
{
public static void main(String[] args)
{
try
{
String xml = "<BookRequest xsi:schemaLocation='http://test/books.xsd'><Book><Name>abc</Name><Author>xyz</Author></Book></BookRequest>";
Map<String, Object> map = new ToMap().convertToMap(xml);
Map<String, Object> map2 = (Map<String, Object>) map.get("BookRequest");
StringBuilder xmlResult = new ToXML().convertToXML(map2, true);
System.out.println(xmlResult);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
输出
-------- XML Detected --------
-------- Map created Successfully --------
-------- Map Detected --------
-------- XML created Successfully --------
<Book><Name>abc</Name><Author>xyz</Author></Book>