2013-08-04 18 views
1

노코 기리의 noblanks 사용 방법 :내가 XML 문서가

<?xml version="1.0"?> 
<installation id="ayfw-a"> 
</installation> 

내가 같이이 문서에 자식 노드를 추가하고 :

data = Nokogiri::XML(IO.read('file')) { |doc| doc.noblanks } 
new_record = Nokogiri::XML::Node.new('tag', data) 
data.root.add_child(new_record) 
File.open('file', 'w') { |dh_file| dh_file.write(data.to_xml(:indent => 4)) } 

을 내 파일 내에서이 얻을이 코드를 :

<?xml version="1.0"?> 
<installation id="ayfw-a"> 
<tag/></installation> 

여기서 noblanks은 작동하지 않습니다. 하지만, 내 파일이 이미 자식 노드를 가지고 새로운 노드를 noblanks 삽입하면 잘 작동하기 전에 경우 : 그래서

<?xml version="1.0"?> 
<installation id="ayfw-a"> 
    <!----> 
    <tag/> 
</installation> 

: 새로운 노드를 삽입 한 후

<?xml version="1.0"?> 
<installation id="ayfw-a"> 
    <!----> 
</installation> 

: 새로운 노드를 삽입하기 전에

noblanks은 이미 "패턴"이 표시된 경우에만 작동합니다. 아직 XML이없는 경우 XML을 올바르게 들여 쓸 수있는 방법이 있습니까?


아마도 noblanks이 사용할 수있는 권한 옵션이 아니라 이미 <installation>에서 일부 노드가있는 경우 어떤 이유로 작동합니다. 기본적으로 자식 노드를 추가하는 것은이 때 내가 현재 가지고있는 :

<?xml version="1.0"?> 
<installation id="ayfw-a"> 
<tag/></installation> 

은 내가이 있어야하는 것은 이것이다 :

<?xml version="1.0"?> 
<installation id="ayfw-a"> 
    <tag/> 
</installation> 

그리고 추가 자식 노드가 어떤 속성을 가진, 비어 있어야하는 I 단순화를 위해 억압된다.

답변

0

당신의 두 가지 예는 완전히 똑같은 행동을 보여 주지만, 그들 중 하나가 다른 것을한다고 말합니다.

지금까지 내가 말할 수있는 빈 노드를 제거 결코 극복 noblanks를 지정 :

xml.xml :

<?xml version="1.0"?> 
<root> 
    <installation id="ayfw-a"></installation> 
    <dog></dog> 
    <cat/> 
</root> 

.

require 'nokogiri' 

data = Nokogiri::XML(IO.read('xml.xml')) { |doc| doc.noblanks } 
puts data 

--output:-- 
<?xml version="1.0"?> 
<root> 
    <installation id="ayfw-a"/> 
    <dog/> 
    <cat/> 
</root> 

나는 출력이 될 것으로 기대합니다 :

물론
<root> 
    <installation id="ayfw-a"></installation> 
</root> 

은 (루비의 전형) 끔찍한 노코 기리 워드 프로세서 빈 노드가 무엇인지 정의하지 않습니다.

<dog></dog> 

에 : 분명히, noblanks가하는 일의 범위는 다음과 같이 노드를 변환입니다

<dog/> 

업데이트

아, 그래서 당신의 문제는 XML의 매우 인쇄 함께 . 좋아, 네가하는 똑같은 문제를 안다. 내가 당신의 질문을 수 있었는지 보여 드리죠 :


나는 문제가 내 XML 내가 원하는 방식으로 포맷하는 데 :

xml.xml을 :

<?xml version="1.0"?> 
<installation id="ayfw-a"> 
</installation> 

합니다.

require 'nokogiri' 

data = Nokogiri::XML(IO.read('xml.xml')) {|doc| doc.noblanks} 
new_record = Nokogiri::XML::Node.new('tag', data) 
data.root.add_child(new_record) 
puts data.to_xml(indent: 4, indent_text: ".") 

--output:-- 
<?xml version="1.0"?> 
<installation id="ayfw-a"> 
<tag/></installation> 

to_xml() 방법이 올바르게 작동하지 않는 것 같습니다. 나는 출력이 될 것으로 예상 :

<?xml version="1.0"?> 
<installation id="ayfw-a"> 
....<tag/> 
</installation> 

그러나 to_xml() 방법은 출력에게 내가 태그가 기존의 자식 노드가있을 때 원하는 방식으로 포맷을 수행합니다

xml.xml :

<?xml version="1.0"?> 
<installation id="ayfw-a"> 
    <dog>Rover</dog> 
</installation> 

.

require 'nokogiri' 

data = Nokogiri::XML(IO.read('xml.xml')) {|doc| doc.noblanks} 
new_record = Nokogiri::XML::Node.new('tag', data) 
data.root.add_child(new_record) 
puts data.to_xml(indent: 4, indent_text: ".") 

--output:-- 
<?xml version="1.0"?> 
<installation id="ayfw-a"> 
....<dog>Rover</dog> 
....<tag/> 
</installation> 

Nokogiri는 출력을 첫 번째 경우에 원하는대로 포맷 할 수 있습니까?


Nokogiri에는 아주 좋은 예쁜 프린터가있는 것처럼 보입니다.

xml.xml :

<?xml version="1.0"?> 
<installation id="ayfw-a"> 
</installation> 

이 REXML가 노코 기리보다 더 나은 꽤 프린터를 것 같다.

require 'nokogiri' 

data = Nokogiri::XML(IO.read('xml.xml')) {|doc| doc.noblanks} 
new_record = Nokogiri::XML::Node.new('tag', data) 
data.root.add_child(new_record) 
puts data.to_xml(indent: 4, indent_text: ".") 

require "rexml/document" 

REXML::Document.new(data.to_xml).write(File.open("output.txt", "w"), indent_spaces = 4) 


--output:-- 
<installation id="ayfw-a"> 
<tag/></installation> 

$ cat output.txt 
<?xml version='1.0'?> 
<installation id='ayfw-a'> 
    <tag/> 
</installation> 
+0

나는 당신의 노력을 좋아합니다. :)) –

+0

@Babai, Thanks! – 7stud

0

예쁜 인쇄 XML은 올바른 XML을 보장하지 않습니다. 단지 "꽤"입니다. Nokogiri는 유효한 XML을 생성하는데 훨씬 더 중요합니다.당신이 특정 시작 형식을 가지고있는 경우 노코 기리 구문 분석하는

, 다음에 구축, 작은 템플릿을 만들 : 약간의 코드를 조정

<?xml version="1.0"?> 
<installation id="ayfw-a"> 
    <tag/> 
</installation> 

:

require 'nokogiri' 

doc = Nokogiri::XML(<<EOT) 
<?xml version="1.0"?> 
<installation id="ayfw-a"> 
    <tag/> 
</installation> 
EOT 

puts doc.to_xml 

생성하는 시작 번호 root 노드 ID와 삽입 태그의 이름을 설정할 수 있습니다.

require 'nokogiri' 

ID = 'ayfw-a' 
TAG = 'foo' 

doc = Nokogiri::XML(<<EOT) 
<?xml version="1.0"?> 
<installation id="#{ ID }"> 
    <#{ TAG }/> 
</installation> 
EOT 

puts doc.to_xml 

개 어떤 출력 :

<?xml version="1.0"?> 
<installation id="ayfw-a"> 
    <foo/> 
</installation> 

이를 작성하는 또 다른 방법은 다음과 같습니다

require 'nokogiri' 

ID = 'ayfw-a' 
TAG = 'foo' 

doc = Nokogiri::XML(<<EOT) 
<?xml version="1.0"?> 
<installation> 
    <tag/> 
</installation> 
EOT 

doc.root['id'] = ID 
doc.at('tag').name = TAG 

puts doc.to_xml 

출력 :

무슨 일이 있어도
<?xml version="1.0"?> 
<installation id="ayfw-a"> 
    <foo/> 
</installation> 

, 당신이이 문제를 해결하고 생산성을 높일 수 있습니다 .

관련 문제