Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用XmlUtils解析XML_Java_Xml_Parsing_Jdom - Fatal编程技术网

Java 用XmlUtils解析XML

Java 用XmlUtils解析XML,java,xml,parsing,jdom,Java,Xml,Parsing,Jdom,我使用XmlUtils解析和提取列表中id属性的值,但它返回空值 我哪里做错了?请建议 XML: 我们开始 import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.StringReader; import java.util.List; import org.jdom.Document; import org.jdom.Element; import org.jdom.

我使用XmlUtils解析和提取列表中id属性的值,但它返回空值

我哪里做错了?请建议

XML: 我们开始

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.xml.sax.InputSource;

public class XmlUtils
{
    public static void main(String[] args) throws JDOMException, IOException {
                String test="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><address-book xmlns=\"qwerty\" xmlns:ab2=\"asdfgh\"><contact time-stamp=\"2014-02-26T16:35:20.678+02:00\" id=\"12345\"></contact><contact time-stamp=\"2014-02-26T16:37:19.370+02:00\" id=\"12346\"></contact><contact time-stamp=\"2014-02-26T16:38:53.345+02:00\" id=\"12347\"></contact><contact time-stamp=\"2014-02-26T16:37:30.828+02:00\" id=\"12348\"></contact></address-book>";
        Document document = XmlUtils.createDocument(test);
        Element rootNode=document.getRootElement();
        Namespace namespace=Namespace.getNamespace("qwerty");
        rootNode.setNamespace(namespace);
        List list = rootNode.getChildren("contact",namespace);
        for (int i = 0; i < list.size(); i++) {
              Element node = (Element) list.get(i);
              System.out.println("id values using Style 1 : " + node.getAttribute("id").getValue());
             }
        List<Element> list2 = document.getRootElement().getChildren("contact",namespace);
        for( Element ele : list2){
            System.out.println(ele.getAttribute("id").getValue());
        }
    }
public static String getFormatedXMLString(String doc)  throws JDOMException, IOException
{
    return (  makeDomToFormatedString( createDocument(doc)  ) ) ;
}

public static String makeDomToFormatedString(Document doc)
{
    return makeDomToFormatedString(doc.getRootElement());
}

public static String makeDomToFormatedString(Element elem)
{
    XMLOutputter output = new XMLOutputter();

    Format format = Format.getPrettyFormat();
    format.setExpandEmptyElements( true );
    format.setTextMode( Format.TextMode.TRIM_FULL_WHITE );

    output.setFormat( format );
    return output.outputString(elem);
}

public static Document createDocument(String xml) throws JDOMException, IOException
{
    InputSource in = new InputSource(new StringReader(xml));
    SAXBuilder saxB = new SAXBuilder();
    return ((saxB.build(in)));
}

public static Element createElement(File xmlFile) throws JDOMException, IOException
{
    SAXBuilder saxB = new SAXBuilder();
    Document document = saxB.build(xmlFile);
    return document.getRootElement();
}


public static void writeXmlFile(Document doc,String path){

    try {
        XMLOutputter xmlOutputer = new XMLOutputter();

        xmlOutputer.setFormat( Format.getPrettyFormat() );
        xmlOutputer.output( doc , new FileWriter( path ) );

    } catch (IOException e) {
      e.printStackTrace();
    }
}
}

如果您遇到任何问题,请告诉我:

我会查看Javadocs for getRootElement-在我看来,它会返回地址簿,在这一点上,查找同样名为AddressBook的孩子将返回一个空列表content@codehitman尝试了这个解决方案-它不适用于me@user3361078您可以编辑xml吗?也可以编辑联系人tagHi的无结束标记,谢谢您的尝试:-但这并没有解决问题。仍然没有输出-与我之前的结果相同。另外,您的示例采用不同的格式-在原始XML中没有结尾,因此它不是有效的XML,无法使用任何解析器进行解析…除非它作为结束标记。。。或者你是对的-有一个-这里是was舒尔内部发生的事情的全部内容-无所谓-只需要行中的id:上面更新的XML结构只有两个选项,要么通过为和添加endtags来生成XML,要么将其作为普通文件读取,从id搜索,然后读取其值Yes,我想我会选择第二个选项,因为我得到了XML,再次感谢您的努力!
Document document = XmlUtils.createDocument(responseString);
List<Element> list = document.getRootElement().getChildren("address-book");
for( Element ele : list){
    System.out.println(ele.getChild("contact").getAttribute("id").getValue());
}
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.xml.sax.InputSource;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.FileWriter;

public class XmlUtils
{
private static final Logger logger = Logger.getLogger(XmlUtils.class);
public static String getFormatedXMLString(String doc)  throws JDOMException, IOException
{
    return (  makeDomToFormatedString( createDocument(doc)  ) ) ;
}

public static String makeDomToFormatedString(Document doc)
{
    return makeDomToFormatedString(doc.getRootElement());
}

public static String makeDomToFormatedString(Element elem)
{
    XMLOutputter output = new XMLOutputter();

    Format format = Format.getPrettyFormat();
    format.setExpandEmptyElements( true );
    format.setTextMode( Format.TextMode.TRIM_FULL_WHITE );

    output.setFormat( format );
    return output.outputString(elem);
}

public static Document createDocument(String xml) throws JDOMException, IOException
{
    InputSource in = new InputSource(new StringReader(xml));
    SAXBuilder saxB = new SAXBuilder();
    return ((saxB.build(in)));
}

public static Element createElement(File xmlFile) throws JDOMException, IOException
{
    SAXBuilder saxB = new SAXBuilder();
    Document document = saxB.build(xmlFile);
    return document.getRootElement();
}


public static void writeXmlFile(Document doc,String path){

    try {
        XMLOutputter xmlOutputer = new XMLOutputter();

        xmlOutputer.setFormat( Format.getPrettyFormat() );
        xmlOutputer.output( doc , new FileWriter( path ) );

    } catch (IOException e) {
       logger.error("cant write xml file",e);
    }
}
}
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.xml.sax.InputSource;

public class XmlUtils
{
    public static void main(String[] args) throws JDOMException, IOException {
                String test="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><address-book xmlns=\"qwerty\" xmlns:ab2=\"asdfgh\"><contact time-stamp=\"2014-02-26T16:35:20.678+02:00\" id=\"12345\"></contact><contact time-stamp=\"2014-02-26T16:37:19.370+02:00\" id=\"12346\"></contact><contact time-stamp=\"2014-02-26T16:38:53.345+02:00\" id=\"12347\"></contact><contact time-stamp=\"2014-02-26T16:37:30.828+02:00\" id=\"12348\"></contact></address-book>";
        Document document = XmlUtils.createDocument(test);
        Element rootNode=document.getRootElement();
        Namespace namespace=Namespace.getNamespace("qwerty");
        rootNode.setNamespace(namespace);
        List list = rootNode.getChildren("contact",namespace);
        for (int i = 0; i < list.size(); i++) {
              Element node = (Element) list.get(i);
              System.out.println("id values using Style 1 : " + node.getAttribute("id").getValue());
             }
        List<Element> list2 = document.getRootElement().getChildren("contact",namespace);
        for( Element ele : list2){
            System.out.println(ele.getAttribute("id").getValue());
        }
    }
public static String getFormatedXMLString(String doc)  throws JDOMException, IOException
{
    return (  makeDomToFormatedString( createDocument(doc)  ) ) ;
}

public static String makeDomToFormatedString(Document doc)
{
    return makeDomToFormatedString(doc.getRootElement());
}

public static String makeDomToFormatedString(Element elem)
{
    XMLOutputter output = new XMLOutputter();

    Format format = Format.getPrettyFormat();
    format.setExpandEmptyElements( true );
    format.setTextMode( Format.TextMode.TRIM_FULL_WHITE );

    output.setFormat( format );
    return output.outputString(elem);
}

public static Document createDocument(String xml) throws JDOMException, IOException
{
    InputSource in = new InputSource(new StringReader(xml));
    SAXBuilder saxB = new SAXBuilder();
    return ((saxB.build(in)));
}

public static Element createElement(File xmlFile) throws JDOMException, IOException
{
    SAXBuilder saxB = new SAXBuilder();
    Document document = saxB.build(xmlFile);
    return document.getRootElement();
}


public static void writeXmlFile(Document doc,String path){

    try {
        XMLOutputter xmlOutputer = new XMLOutputter();

        xmlOutputer.setFormat( Format.getPrettyFormat() );
        xmlOutputer.output( doc , new FileWriter( path ) );

    } catch (IOException e) {
      e.printStackTrace();
    }
}
}
id values using Style 1 : 12345
id values using Style 1 : 12346
id values using Style 1 : 12347
id values using Style 1 : 12348
12345
12346
12347
12348