Java将XML打印到控制台
我有以下代码:Java将XML打印到控制台,java,xml,Java,Xml,我有以下代码: import java.net.URL; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; public class Main { private static Document loadTestDocument(String url) throws Exception { DocumentBuilderFactory factory = Do
import java.net.URL;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
public class Main {
private static Document loadTestDocument(String url) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
return factory.newDocumentBuilder().parse(new URL(url).openStream());
}
public static void main(String[] args) throws Exception {
Document doc = loadTestDocument("https://www.w3schools.com/xml/note.xml");
System.out.println(doc.getElementsByTagName("note").item(0).getChildNodes().item(0).getNodeValue());
}
}
就我而言,这应该将节点文本打印到java控制台,但它似乎什么也不打印。。。没有任何错误。
我做错了什么?执行以下操作以获取文本内容
doc.getElementsByTagName("note").item(0).getChildNodes().item(0).getTextContent()
编辑
试试这个,有一些空节点
int i;
for(i = 0; i < doc.getElementsByTagName("note").item(0).getChildNodes().getLength(); i++){
System.out.println(doc.getElementsByTagName("note").item(0).getChildNodes().item(i).getTextContent());
}
执行以下操作以获取文本内容
doc.getElementsByTagName("note").item(0).getChildNodes().item(0).getTextContent()
编辑
试试这个,有一些空节点
int i;
for(i = 0; i < doc.getElementsByTagName("note").item(0).getChildNodes().getLength(); i++){
System.out.println(doc.getElementsByTagName("note").item(0).getChildNodes().item(i).getTextContent());
}
零项是注释。它没有价值。请使用上面的代码打印Tove
System.out.println(doc.getElementsByTagName("note").item(0).getChildNodes().item(1).getTextContent());
零项是注释。它没有价值。请使用上面的代码打印Tove
System.out.println(doc.getElementsByTagName("note").item(0).getChildNodes().item(1).getTextContent());
试试这个
Document doc = loadTestDocument("https://www.w3schools.com/xml/note.xml");
doc.getDocumentElement().normalize();
Element element =(Element)doc.getElementsByTagName("note").item(0);
System.out.println(element.getElementsByTagName("to").item(0).getTextContent());
System.out.println(element.getElementsByTagName("from").item(0).getTextContent());
输出:
<note>
<to>
Tove
</to>
<from>
Jani
</from>
<heading>
Reminder
</heading>
<body>
Don't forget me this weekend!
</body>
</note>
托夫
Jani试试这个
Document doc = loadTestDocument("https://www.w3schools.com/xml/note.xml");
doc.getDocumentElement().normalize();
Element element =(Element)doc.getElementsByTagName("note").item(0);
System.out.println(element.getElementsByTagName("to").item(0).getTextContent());
System.out.println(element.getElementsByTagName("from").item(0).getTextContent());
输出:
<note>
<to>
Tove
</to>
<from>
Jani
</from>
<heading>
Reminder
</heading>
<body>
Don't forget me this weekend!
</body>
</note>
托夫
Jani使用另一个api Jsoup,我们可以解析xml并在控制台上以字符串形式打印整个xml 请使用以下代码段尝试此操作:
org.jsoup.nodes.Document document = Jsoup.parse(new URL("https://www.w3schools.com/xml/note.xml"), 5000);
System.out.println("XML content : "+document.html());
输出:
<note>
<to>
Tove
</to>
<from>
Jani
</from>
<heading>
Reminder
</heading>
<body>
Don't forget me this weekend!
</body>
</note>
使用另一个api Jsoup,我们可以在控制台上解析xml并将整个xml打印为字符串 请使用以下代码段尝试此操作:
org.jsoup.nodes.Document document = Jsoup.parse(new URL("https://www.w3schools.com/xml/note.xml"), 5000);
System.out.println("XML content : "+document.html());
输出:
<note>
<to>
Tove
</to>
<from>
Jani
</from>
<heading>
Reminder
</heading>
<body>
Don't forget me this weekend!
</body>
</note>
你真的试过调试吗?你真的试过调试吗?嗨,桑德普。“getTextContent”的结果完全相同。@Pawel我已经编辑了答案。嗨,Sandeep。“getTextContent”的结果完全相同。@Pawel我已经编辑了答案。为什么答案总是这么简单?:谢谢你Loljeenewy为什么答案总是这么简单谢谢你,罗琳