我在stackoverflow上看到了很多类似的问题并尝试了很多但仍然没有成功。所以发布我的问题。
这是我的计划:
See my class - AppExtension.swift
// MARK: - UIImage (Base64 Encoding)
public enum ImageFormat {
case PNG
case JPEG(CGFloat)
}
extension UIImage {
public func base64(format: ImageFormat) -> String {
var imageData: NSData
switch format {
case .PNG: imageData = UIImagePNGRepresentation(self)
case .JPEG(let compression): imageData = UIImageJPEGRepresentation(self, compression)
}
return imageData.base64EncodedStringWithOptions(.allZeros)
}
}
我的程序片段如下:
CGSize dimensions = [result defaultRepresentation].dimensions;
CLLocation *loc = [result valueForProperty:ALAssetPropertyLocation];
NSDate *date = [result valueForProperty:ALAssetPropertyDate];
[assets addObject:@{
@"node": @{
@"type": [result valueForProperty:ALAssetPropertyType],
@"group_name": [group valueForProperty:ALAssetsGroupPropertyName],
@"image": @{
@"uri": uri,
@"height": @(dimensions.height),
@"width": @(dimensions.width),
@"isStored": @YES,
},
@"timestamp": @(date.timeIntervalSince1970),
@"location": loc ?
@{
@"latitude": @(loc.coordinate.latitude),
@"longitude": @(loc.coordinate.longitude),
@"altitude": @(loc.altitude),
@"heading": @(loc.course),
@"speed": @(loc.speed),
} : @{},
}
}];
此处文档返回null。我不确定我错过了什么。
答案 0 :(得分:1)
org.w3c.dom.Node#getTextContent
方法承诺在调用时返回null
:
DOCUMENT_NODE
DOCUMENT_TYPE_NODE
NOTATION_NODE
请参阅文档here。
如果您想验证自己的Document
包含预期的内容,或者通常在其节点上进行迭代,则可以迭代getChildNodes
。
这是一个小的递归方法,在一个自包含的示例中打印该XML字符串的一些调试信息。
package test;
import java.io.ByteArrayInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Main {
public static void main(String[] args) throws Exception {
String test = "<RESULTS><DEVICE name=\"qwewewew\"><PORT name=\"ilo\"><DB_SOURCE>abncd</DB_SOURCE><WIRE_TYPE>ilo</WIRE_TYPE><CONNECTS>abncd</CONNECTS></PORT><PORT name=\"onboard-1\"><DB_SOURCE>abncd</DB_SOURCE><WIRE_TYPE>net</WIRE_TYPE><CONNECTS>abncd</CONNECTS></PORT><PORT name=\"abncd\"><DB_SOURCE>abncd</DB_SOURCE><WIRE_TYPE/><CONNECTS/></PORT><PORT name=\"abncd\"><DB_SOURCE>abncd</DB_SOURCE><WIRE_TYPE>fiber</WIRE_TYPE><CONNECTS>abncd</CONNECTS></PORT><PORT name=\"power\"><DB_SOURCE>abncd</DB_SOURCE><WIRE_TYPE/><CONNECTS/></PORT><PORT name=\"serial\"><DB_SOURCE>abncd</DB_SOURCE><WIRE_TYPE>serial</WIRE_TYPE><CONNECTS>abncd</CONNECTS></PORT></DEVICE></RESULTS>";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new ByteArrayInputStream(test.getBytes("UTF-8")));
NodeList list = document.getChildNodes();
recurse(list);
}
static void recurse(NodeList list) {
if (list == null || list.getLength() == 0) {
return;
}
else {
for (int i = 0; i < list.getLength(); i++) {
Node item = list.item(i);
System.out.println(item);
recurse(item.getChildNodes());
}
}
}
}
<强>输出强>
[RESULTS: null]
[DEVICE: null]
[PORT: null]
[DB_SOURCE: null]
[#text: abncd]
[WIRE_TYPE: null]
[#text: ilo]
[CONNECTS: null]
[#text: abncd]
[PORT: null]
[DB_SOURCE: null]
[#text: abncd]
[WIRE_TYPE: null]
[#text: net]
[CONNECTS: null]
[#text: abncd]
[PORT: null]
[DB_SOURCE: null]
[#text: abncd]
[WIRE_TYPE: null]
[CONNECTS: null]
[PORT: null]
[DB_SOURCE: null]
[#text: abncd]
[WIRE_TYPE: null]
[#text: fiber]
[CONNECTS: null]
[#text: abncd]
[PORT: null]
[DB_SOURCE: null]
[#text: abncd]
[WIRE_TYPE: null]
[CONNECTS: null]
[PORT: null]
[DB_SOURCE: null]
[#text: abncd]
[WIRE_TYPE: null]
[#text: serial]
[CONNECTS: null]
[#text: abncd]