I'm building an android app that communicates to a web server and am struggling with the following scenario:
我正在构建一个与web服务器通信的android应用程序,并在与以下场景作斗争:
Given ONE line of XML in a String eg:
给定字符串中的一行XML如:
"<test one="1" two="2" />"
I would like to extract the values into a HashMap so that:
我想将这些值提取到HashMap中,以便:
map.get("one") = "1"
map.get("two") = "2"
I already can do this with a full XML document using the SAX Parser, this complains when i try to just give it the above string with a MalformedUrlException: Protocol not found
对于使用SAX解析器的完整XML文档,我已经可以这样做了,当我尝试使用未找到的MalformedUrlException: Protocol给它提供上述字符串时,会出现这种情况
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
Document doc = null;
builder = factory.newDocumentBuilder();
doc = builder.parse("<test one="1" two="2" />"); //here
I realize some regex could do this but Id really rather do it properly.
我意识到一些正则表达式可以做到这一点,但我确实需要正确地做。
The same behaviour can be found at http://metacpan.org/pod/XML::Simple#XMLin which is what the web server uses.
同样的行为可以在http://metacpan.org/pod/XML: Simple#XMLin找到,这是web服务器使用的。
Can anyone help? Thanks :D
谁能帮忙吗?谢谢你:D
1 个解决方案
#1
5
DocumentBuilder.parse(String)
treats the string as a URL. Try this instead:
DocumentBuilder.parse(String)将字符串视为URL。试试这个:
Document doc = builder.parse(new InputSource(new StringReader(text)));
(where text
contains the XML, of course).
(当然,文本包含XML)。
#1
5
DocumentBuilder.parse(String)
treats the string as a URL. Try this instead:
DocumentBuilder.parse(String)将字符串视为URL。试试这个:
Document doc = builder.parse(new InputSource(new StringReader(text)));
(where text
contains the XML, of course).
(当然,文本包含XML)。