【トラブル】【Java】【XML】 DOM に関わるトラブルシューティング

■ エラー「java.net.MalformedURLException: no protocol」が発生する

 * 以下のサンプルを実行すると、
   エラー「java.net.MalformedURLException: no protocol」(詳細は以下のエラー内容を参照のこと)
   が発生する

エラー内容

java.net.MalformedURLException: no protocol: <?xml version="1.0" encoding="UTF-8"?><XmlRoot><value></value></XmlRoot>
	at java.net.URL.<init>(URL.java:586)
	at java.net.URL.<init>(URL.java:483)
	at java.net.URL.<init>(URL.java:432)
	at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
	at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
	at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
	at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
	at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
	at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
	at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
	at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:177)
	at com.sample.SampleXml.main(SampleXml.java:19)

サンプル

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class SampleXml {

   public static void main(String[] args) {
      String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><XmlRoot><value></value></XmlRoot>";
      try {
         // To Create a factory
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
         // Use the factory to create a builder
         DocumentBuilder builder = factory.newDocumentBuilder();
         // ★ここでエラーが発生する★
         Document doc = builder.parse(xml);
         // To get a list of all elements in the document
         NodeList list = doc.getElementsByTagName("*");

         // Output
         System.out.println("XML Elements : ");
         for (int i = 0; i < list.getLength(); i++) {
            Element element = (Element) list.item(i);
            System.out.println(element.getNodeName());
         }
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

解決策

* 「builder.parse(new InputSource(new ByteArrayInputStream(xml.getBytes('UTF-8'))))」とする
import java.io.ByteArrayInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class SampleXml {

   public static void main(String[] args) {
      String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><XmlRoot><value></value></XmlRoot>";
      try {
         // To Create a factory
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
         // Use the factory to create a builder
         DocumentBuilder builder = factory.newDocumentBuilder();
         // ★ここを修正★
         Document doc = builder.parse(new InputSource(new ByteArrayInputStream(xml.getBytes("UTF-8"))));
         // 後は同じなので省略

修正後の出力結果

XML Elements : 
XmlRoot
value

参考文献

http://stackoverflow.com/questions/1706493/java-net-malformedurlexception-no-protocol