안드로이드 개발 질문/답변
(글 수 358)
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory. newInstance ( ) ;
DocumentBuilder dBuilder = dbFactory. newDocumentBuilder ( ) ;
Document doc = dBuilder. parse ( new String(resbuf.array()) ) ;
doc. getDocumentElement ( ) . normalize ( ) ;
System . out . println ( "Root element :" + doc. getDocumentElement ( ) . getNodeName ( ) ) ;
NodeList nList = doc. getElementsByTagName ( "NHGPMSG" ) ;
System . out . println ( "-----------------------" ) ;
for ( int temp = 0 ; temp < nList. getLength ( ) ; temp ++ ) {
Node nNode = nList. item ( temp ) ;
if ( nNode. getNodeType ( ) == Node. ELEMENT_NODE ) {
Element eElement = ( Element ) nNode ;
System . out . println ( "NDEFTagRecord : " + getTagValue ( "NDEFTagRecord" , eElement ) ) ;
}
}
} catch ( Exception e ) {
e. printStackTrace ( ) ;
System . out . println ("\n------------------------------------\n" +"e : " + e +"\n------------------------------------") ;
}
이런식으로 컨트롤 + cv로 가져와서 수정을해놧습니다.
Document doc = dBuilder. parse ( new String(resbuf.array()) ) ; 이부분이 xml가져오는부분인대
new String(resbuf.array()) 이부분이 서버에서 xml을 받아오는 부분입니다.
근대 parse 라는것은 URI or File 을 불러와서 하는건대 ...
new String(resbuf.array()) 같은경우는 서버요청을하고 그요청에 답변이 담겨져 있는겁니다.
new String(resbuf.array()) xml이 String로 담겨져있는걸 필요한부분만 가져오려합니다.
뭐 노가다해서 xml에 있는거 서브스티링이나 해서 하나하나 잘라가면서 가져와도 되거나
따로 File로 저장해버려서 File로 불러와도 되는대
그것보다 다른더좋은 방법이 있을꺼라 믿고있어서 이렇게 질문올립니다.
헤헿
참고사항
e : java.net.MalformedURLException: no protocol: <?xml version="1.0" encoding="UTF-8"?>
<NHGPMSG version="1.0">
<Header>
<Endpoint IN="010-1234-5678" model="GalaxyNexus" OS="android"/>
<Result code="0" desc="OK" context=""/>
</Header>
<Body>
<TagQueryResponse>
<NDEFTagResult code="urn:mcode:id:urn:mcode:id:2730.6.1.10006.633">
<NDEFTagRecord order="0" preference="100" flag="u" service="C2U+ktmcs:web" value="http://csnfctb.co.cc:9201/taginfo.do?type=html&tagid=20120423175913_841" title=""/>
</NDEFTagResult>
</TagQueryResponse>
</Body>
</NHGPMSG>
이것이 저부분 에러를 찍었을때 나는 에러입니다 ㅠㅠ
2012.05.11 15:45:42
고대로 복사해서 쓰셔도 되고... 안의 내용만 써도 됩니다만..
여러군데에서 쓰신다면 공용class 파일을 하나 만들어 놓고 호출해서 써도 되겠죠..
어쨌든 지금 에러나는 클래스에 메서드를 복사하시고
Document doc = dBuilder. parse ( new String(resbuf.array()) ) ;
부분을
Document doc = stringToDocument( new String(resbuf.array())); 으로 변경 하시면 될겁니다..
아래 뭐하는거죠?
라고 물어보신건 리턴 값 입니다.
public static Document stringToDocument(final String xmlSource) throws SAXException, ParserConfigurationException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new InputSource(new StringReader(xmlSource)));
}
이 메서드에 XML String 값을 넣으시면 Document 형태로 튀어 나옵니다. :)