SAX 파서를 사용하여 XML 문서를 구문 분석하려고합니다. 문서에 네임 스페이스가 포함되어 있지 않으면 문서가 완벽하게 작동합니다. 그러나 루트 요소에 네임 스페이스를 추가하면 NullPointerException이 발생합니다.Java SAX Parser 네임 스페이스 예외 NullPointerException
가 여기 내 작업 XML 문서입니다 :
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Date>01102013</Date>
<ID>1</ID>
<Count>3</Count>
<Items>
<Item>
<Date>01102013</Date>
<Amount>100</Amount>
</Item>
<Item>
<Date>02102013</Date>
<Amount>200</Amount>
</Item>
</Items>
</Root>
이 문제가있는 버전입니다 :
여기<?xml version="1.0" encoding="utf-8"?>
<Root xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.xyz.com/q">
<Date>01102013</Date>
<ID>1</ID>
<Count>3</Count>
<Items>
<Item>
<Date>01102013</Date>
<Amount>100</Amount>
</Item>
<Item>
<Date>02102013</Date>
<Amount>200</Amount>
</Item>
</Items>
</Root>
내 코드입니다 :
Document doc = null;
SAXBuilder sax = new SAXBuilder();
sax.setFeature("http://xml.org/sax/features/external-general-entities", false);
// I set this as true but still nothing changes
sax.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
sax.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
doc = sax.build(xmlFile); // xmlFile is a File object which is a function parameter
Root root = new Root();
Element element = doc.getRootElement();
root.setDate(element.getChild("Date").getValue());
root.setID(element.getChild("ID").getValue());
.
.
.
내가 처음 XML을 사용
, 잘 작동하고 있습니다. 두 번째 XML을 사용할 때element.getChild("Date").getValue()
을 반환합니다.
참고 : 나는 여전히 루트 요소를 액세스 할 수 있습니다
doc.getRootElement().getNamespaceURI();
를 사용하여 "http://www.xyz.com/q"부분을 읽을 수.
누구나이를 극복하는 방법을 알고 있습니까?
미리 감사드립니다.
나는 같이 넣어했습니다 그것은 일 : element.getChild ("날짜"문서를 .getNamespace()). getValue() 답변 해 주셔서 감사합니다! –