我从服务器收到一个XML
字符串,其结构如下:
<item_list>
<category>
<item id="1" name="name1" base64="base64String1" />
<item id="2" name="name2" base64="base64String2" />
<item id="3" name="name3" base64="base64String3" />
<item id="4" name="name4" base64="base64String4" />
........
</category>
<category>
........
</category>
<item_list>
我需要做的是:当我第一次读取字符串时,读取Base64
字符串,将它们保存在文件中,然后从该字符串中删除它们。然后将此字符串写入XML
文件。因此,此XML
将如下所示:
<item_list>
<category>
<item id="1" name="name1" />
<item id="2" name="name2" />
<item id="3" name="name3" />
<item id="4" name="name4" />
........
</category>
<category>
........
</category>
<item_list>
我使用哪个解析器来阅读和删除Base64
字符串?哪个是最好的?
如果有人能给我一个例子或教程如何做到这一点,那就太好了。
答案 0 :(得分:2)
您可以使用XPath表达式查询“// * [@ base64]”。
try {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse(new File("your_xml.xml"));
XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "//*[@base64]";
NodeList nodes = (NodeList) xpath.evaluate(expression, document, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
String base64 = element.getAttribute("base64");
element.removeAttribute("base64");
}
String newXML = getStringFromNode(document);
}
catch (Exception e) {
}
getStringFromNode方法
public static String getStringFromNode(Node root) {
StringBuilder result = new StringBuilder();
if (root.getNodeType() == 3)
result.append(root.getNodeValue());
else {
if (root.getNodeType() != 9) {
StringBuffer attrs = new StringBuffer();
for (int k = 0; k < root.getAttributes().getLength(); ++k) {
attrs.append(" ").append(
root.getAttributes().item(k).getNodeName()).append(
"=\"").append(
TextUtils.htmlEncode(root.getAttributes().item(k).getNodeValue()))
.append("\" ");
}
result.append("<").append(root.getNodeName()).append(" ")
.append(attrs).append(">");
} else {
result.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
}
NodeList nodes = root.getChildNodes();
for (int i = 0, j = nodes.getLength(); i < j; i++) {
Node node = nodes.item(i);
result.append(getStringFromNode(node));
}
if (root.getNodeType() != 9) {
result.append("</").append(root.getNodeName()).append(">");
}
}
return result.toString();
}
<强> EDITED 强>
同样可以用于解析String。请参阅下面的方法,它将String解析为DOM Document。现在,您可以将此Document用作XPath的输入。
public static Document ReadDocument(String xml)
{
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
StringReader reader = new StringReader(xml);
is.setCharacterStream(reader);
doc = db.parse(is);
} catch (Exception e) {
Log.e("Error", "Error while Parsing Document", e);
return null;
}
return doc;
}
答案 1 :(得分:0)
Vivek功能的微小改进 - &gt;处理CDATA +使用字段常量:
public static String getStringFromNode(Node root) throws IOException
{
StringBuilder result = new StringBuilder();
if (root.getNodeType() == Node.TEXT_NODE)
result.append(root.getNodeValue());
else if(root.getNodeType() == Node.CDATA_SECTION_NODE)
result.append("<![CDATA["+root.getNodeValue()+"]]>");
else {
if (root.getNodeType() != Node.DOCUMENT_NODE) {
StringBuffer attrs = new StringBuffer();
for (int k = 0; k < root.getAttributes().getLength(); ++k) {
attrs.append(" ").append(
root.getAttributes().item(k).getNodeName()).append(
"=\"").append(
root.getAttributes().item(k).getNodeValue())
.append("\" ");
}
result.append("<").append(root.getNodeName()).append(" ")
.append(attrs).append(">");
} else {
result.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
}
NodeList nodes = root.getChildNodes();
for (int i = 0, j = nodes.getLength(); i < j; i++) {
Node node = nodes.item(i);
result.append(getStringFromNode(node));
}
if (root.getNodeType() != Node.DOCUMENT_NODE) {
result.append("</").append(root.getNodeName()).append(">");
}
}
return result.toString();
}